C++ regex escaping punctional characters like "."
Solution 1
As it turns out, the actual problem was due to the way sregex_token_iterator
was used. Using match_default
meant it was always finding the next match in the string, if any, even if there is a non-match in-between. That is,
string source = "AAA.BBB";
regex dot("\\.");
sregex_token_iterator wit(source.begin(), source.end(), dot, regex_constants::match_default);
would give a match at the dot, rather than reporting that there was no match.
The solution is to use match_continuous
instead.
Solution 2
This is because \.
is interpreted as an escape sequence, which the language itself is trying to interpret as a single character. What you want is for your regex to contain the actual string "\.", which is written \\.
because \\
is the escape sequence for the backslash character (\).
Related videos on Youtube
Tim
Updated on September 17, 2022Comments
-
Tim over 1 year
Matching a "." in a string with the
std::tr1::regex
class makes me use a weird workaround.Why do I need to check for "\\\\." instead of "\\."?
regex(".") // Matches everything (but "\n") as expected. regex("\\.") // Matches everything (but "\n"). regex("\\\\.") // Matches only ".".
Can someone explain me why? It's really bothering me since I had my code written using
boost::regex
classes, which didn't need this syntax.Edit: Sorry,
regex("\\\\.")
seems to match nothing.Edit2: Some code
void parser::lex(regex& token) { // Skipping whitespaces { regex ws("\\s*"); sregex_token_iterator wit(source.begin() + pos, source.end(), ws, regex_constants::match_default), wend; if(wit != wend) pos += (*wit).length(); } sregex_token_iterator it(source.begin() + pos, source.end(), token, regex_constants::match_default), end; if (it != end) temp = *it; else temp = ""; }
-
Tim over 11 yearsSee my example code, it just doesn't work the way you said. Besides it seems like "\\\\." doesn't work either.
-
Jakub Zaverka over 11 years@Tim can you show us the whole code you are using to match the string?
-
Tim over 11 yearsI did add the primary function. Thanks for helping out.
-
Agentlien over 11 yearsFor posterity: Tim had some formatting issues (SO interpreting \\ as \ inside posts) which led to the confusion behind my answer. However, having tried myself, \\. does work as expected in Visual Studio 2012, using std::regex.
-
Tim over 11 yearsActually it's not quite the problem. The problem was that
it
doesn't matchend
since the string contains still characters. So a simple replacement ofif (it != end)
toif (it != end && *it != "")
fixed everything!