views:

180

answers:

3

Hi , I am finding Email ids in mu project, where I am preprocessing the input using some Regular Expression.

RegExpPhone6.RegComp("[\[\{\(][ -]?[s][h][i][f][t][ -]?[+-][2][ -]?[\]\}\)]");  

Here while I am compiling i am getting a warning msg like

Warning 39  warning C4129: ')' : unrecognized character escape sequence 

How can i resolve this ? Why this is occuring and Where will it affect? Kindly help me...

A: 

Are you sure you didn't use a double quote within string?

Solve the problem using a binary search. Get rid of half the characters and see if it compiles. If it does compile, repeat with the other half. Then repeat using the half of the non-compiling string until you find the character that causes the problem. Then escape it using a backslash.

E.g. if you want to use a double-quote in a string, you have to write this:

"My name is \"Pat\""
Patrick
+1  A: 

I am not sure with what kind of regex engine you are using, but according to error,

) is unrecognized escape, so its seems complaining about you doing \).

Most of the cases, () and {} inside [] wouldn't need to escape just [(){}] would be fine.

and [s][h][i][f][t] would be same with shift

So, It will be like this

RegExpPhone6.RegComp("[\[{(][ -]?shift[ -]?[+-][2][ -]?[\]})]");    

And

If it still doesn't work, try to change \ to \\, sometimes need to escape backslashes.

S.Mark
+1. BTW, you *always* need to escape backslashes.
Vulcan Eager
A: 

You have escaped characters in your character class that need (and should) not be escaped, especially (), [] and {}.

In your case

RegExpPhone6.RegComp("[{([][ -]?shift[ -]?[+-][2][ -]?[]})]");

should do nicely.

Note that the order of the characters inside the character class matters for this to work.

[][(){}] will match all braces/parens/brackets. But it might throw people off ("is that an empty character class?"), so in that case, escaping the brackets might be a better idea:

RegExpPhone6.RegComp("[{(\[][ -]?shift[ -]?[+-][2][ -]?[\]})]");

See also Jan Goyvaerts' blog about why and where this can be a problem.

Tim Pietzcker