C ++正则表达式转义标点字符,例如“。”。 [英] C++ regex escaping punctional characters like "."
问题描述
匹配。在带有 std :: tr1 :: regex
类的字符串中,我使用了一种怪异的解决方法。
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 ".".
有人可以解释我为什么吗?这真的让我感到困扰,因为我的代码是使用 boost :: regex
类编写的,不需要这种语法。
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.
编辑:抱歉, regex( \。)
似乎不匹配。
Edit2:一些代码
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 = "";
}
推荐答案
事实证明,实际问题是由于使用 sregex_token_iterator
的方式引起的。使用 match_default
意味着即使字符串之间存在不匹配项,它始终会在字符串中查找下一个匹配项(如果有)。也就是说,
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.
解决方案是改用 match_continuous
。
这篇关于C ++正则表达式转义标点字符,例如“。”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!