我们有以下正则表达式来解析方程式5x+10x^3-10x^2
:
[+-]?[\d(x)]*[\^\d]*
下面的代码c++
,取自示例并针对任务进行修改,导致无限循环:
std::regex words_regex("[+-]?[\d(x)]*[\^\d]*"); auto words_begin = std::sregex_iterator(s.begin(), s.end(), words_regex); auto words_end = std::sregex_iterator(); for (std::sregex_iterator i = words_begin; i != words_end; ++i) { std::smatch match = *i; std::string match_str = match.str(); std::cout << match_str << '\n'; }
它也会在编译时抛出警告:
1.cpp:21:35: warning: unknown escape sequence '\d' [-Wunknown-escape-sequence] std::regex words_regex("[+-]?[\d(x)]*[\^\d]*"); ^~ 1.cpp:21:43: warning: unknown escape sequence '\^' [-Wunknown-escape-sequence] std::regex words_regex("[+-]?[\d(x)]*[\^\d]*"); ^~ 1.cpp:21:45: warning: unknown escape sequence '\d' [-Wunknown-escape-sequence] std::regex words_regex("[+-]?[\d(x)]*[\^\d]*");
如果我们天真地将正则表达式转换为[+-]?[d(x)]*[^d]*
- 无限循环离开当然.
如何正确转换正则表达式c++
?
UPD:Clang版本:
Mac:concurrent macbook$ clang++ -v Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn) Target: x86_64-apple-darwin14.5.0 Thread model: posix
Jorge Israel.. 5
问题是\d
尝试将其解释为转义序列,因此您必须转义反斜杠,如\\d
.
另一种方法是使用原始字符串文字,如:
std::regex words_regex(R"([+-]?[\d(x)]*[\^\d]*)");
在这里看到它.
问题是\d
尝试将其解释为转义序列,因此您必须转义反斜杠,如\\d
.
另一种方法是使用原始字符串文字,如:
std::regex words_regex(R"([+-]?[\d(x)]*[\^\d]*)");
在这里看到它.