开发者

Using Regular Expression in VC++

开发者 https://www.devze.com 2022-12-21 09:42 出处:网络
I am finding Email ids in mu project, where I am preprocessing the input using some Regular Expression.

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 esca开发者_运维百科pe sequence 

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


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.


The C++ compiler will convert certain escape sequences in string literals into alternate values. The best known of these is probably \n, which gets converted to a linefeed character. In this case, the compiler is complaining because \) is not one of the standard set of escape sequences (A list of which can be found here, among other places). Since you want the regular expression itself to contain a backslash, you probably want to use \\, which is the escape sequence that converts to a single \. Something like this:

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


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.


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\""
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号