问题
I want to declare the same regex pattern for both languages. For TCL I do this
set pattern "\d\s\S"
but for C++ I have to do this for the same pattern
boost::regex pattern("\\d\\s\\S");
otherwise C++ compiler will tell us the following:
warning C4129: 'd' : unrecognized character escape sequence
so why TCL don't try to find \d \s \S escape symbols and just ignores \
-s but C++ tries and sucks?
P.S. PHP works as TCL as I remeber.
回答1:
This is just how C++ and PHP differ; in PHP, the character following a backslash is matched against a small set of special characters (I believe "rnvtx"
). If the match fails it will just continue without altering the meaning.
However, C++ expects the character to be in that small set (I think the set is bigger btw) but if the match fails you will see an error instead.
回答2:
C++ has the concept of Character Escape Sequences. Escape sequences, which take the form \c
(the 'c' being a character), are used to define certain special characters within string literals, so it follows that backslashes by themselves must also be escaped to denote that a special character isn't being implied.
来源:https://stackoverflow.com/questions/13986708/why-must-backslashes-in-string-literals-be-escaped-in-c