tags:

views:

95

answers:

3

Hi,

I have created one regular expression which is supposed to match for line starting with Project followed by either # or : or - followed by 1 to 3 digit number or simply Title. For example, following lines should get matched

Project # 1

Project#1

Project :1

Project-123

Project Title


but following should not match

Project ABCD

Project*978

My Project

Projects handled


My regular expression is as follows :

^(\s)*?((Project( )*?(#|:|-| )( )*?(\d){1,3})|(PROJECT( )*?(#|:|-| )( )*?(\d){1,3})|Project Title|PROJECT TITLE)\b

Project keyword should be at the start of the line.

For some text, this regular expression is working fine. But this regular expression is matching following line :

Projects Handled:

I have no clue why it is happening. Can anyone find out what is wrong with my regular expression?

I am using C# to do this.

Thanks in advance !!!

+1  A: 

How about like this pattern?

@"Project\s*[#: -]\s*(?:\d+|[A-Z][a-z]+)"

Its does not match to

Project ABCD

Project*978

My Project

Projects handled

But will match to following patterns

Project # 1

Project#1

Project :1

Project-123

Project Title

There is a confusion part in Project Title and Project ABCD though. I assumed you only want title case.

S.Mark
+1  A: 

This works for me:

Regex project = new Regex(@"^\s*?(?:Project *[#:\- ] *(\d){1,3}|Project Title)",
    RegexOptions.ECMAScript | RegexOptions.IgnoreCase | RegexOptions.Multiline);
Rubens Farias
+1  A: 
^\s*Project\s*([\-#:]\s*\d{1,3}|Title)\b

This allows optional spaces before and after Project and between -#: and the three digit number

Amarghosh