Click here to Skip to main content
15,917,481 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi All,

I have a pattern that I need to match.
The pattern needs to capture the below matches :-

GOBACK.
GOBACK .
GOBACK

but ignore the match :-
GOBACK123

I have written the regex in Expresso as (\b(GOBACK)\b[.\s]?)\s+ and it works fine there.
But when I use the same in .NET, it is not capturing the first one.
Please help.
Posted
Comments
Sergey Alexandrovich Kryukov 25-Jul-13 1:37am    
For future, please keep in mind: giving few examples does not mean comprehensive specification. For example, from your description it's not clear if "GOBACK .", "GOBACK .", "GOBACK .", and so on (different number of spaces), should match or not. It could be figured out from your Regex pattern, but it makes no sense because you claim yourself that it can be mistaken. You really need to be very formal with such questions.
—SA
ArunRajendra 25-Jul-13 1:45am    
Can you post your .Net code? I tried the following code and it works.

string s=@"GOBACK.
GOBACK .
GOBACK
GOBACK123
";
Regex r = new Regex(@"(\b(GOBACK)\b[.\s]?)\s+");

MatchCollection m = r.Matches(s);

One bug I can see immediately is: you forgot to escape the dot ('.') using '\.'. The dot needs to be escaped because in Regular Exception it means "any character", not dot.

—SA
 
Share this answer
 
Comments
Jashobanta 25-Jul-13 6:15am    
HI Sergey,

Though I did not see your comment before fixing, but you were right. I modified the end to \\b[\\.\\s] and it worked fine. Thanks everyone.
Sergey Alexandrovich Kryukov 25-Jul-13 10:46am    
You are welcome.
If so, will you accept this answer formally (green button)?
—SA
I think I'd remove the \s+ from the end, but you really haven't explained what you are trying to do.
 
Share this answer
 
Comments
Jashobanta 25-Jul-13 1:46am    
Hi,
What I have to do is to capture the matching sequences in the input string for example, in the input
GOBACK.
GOBACK .
GOBACK
GOBACK123

I need to capture only those lines which either contain a dot or a space after the pattern and not to capture those which contain anything other than that.
ie I need to capture only the first three lines but not the last one.

The problem is, in Expresso I am getting exactly 3 matches, but in Visual Studio the same Regex is not capturing a single match.I ran it by removing the \s+ from the end, but still not works.
I have tried this regEx.

Its working fine with all your test cases.

C#
static void Main(string[] args)
       {
           string text = Console.ReadLine();
           Match match = Regex.Match(text, "\\bGOBACK\\b[\\.\\s]?", RegexOptions.IgnoreCase);

           if (match.Success)
           {
               Console.WriteLine("success");
           }
           else
           {
               Console.WriteLine("Failure");
           }
           Console.ReadLine();
       }
 
Share this answer
 
v2
I changed the expression to

patPrimaryVerb="[\\s]\\b("+primaryVerbList+")\\b[\\.\\s]";

where primaryVerbList contains 'GOBACK' and it worked fine. Thanks everyone for the help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900