我想写一个匹配特定的符号和ascii表中的符号范围的正则表达式。这个正则表达式应该是相当复杂的,但我没有立即创建它,所以我决定先做一个更简单的正则表达式,但是这次我失败了。所以我问这个简单的正则表达式。C++ 11正则表达式范围理解
#include <iostream>
#include <string>
#include <regex>
int main()
{
std::string s ("$");
std::smatch m;
std::regex e ("([\\x21\\x23-\\x25]+)");
std::regex_search (s,m,e);
for (int i = 1; i < m.size(); i++)
{
std::cout << m[i] << " ";
}
std::cout << std::endl;
return 0;
}
在这里,在正则表达式中使用的ASCII码:
Decimal Octal Hex Binary Value
033 041 021 00100001 ! (exclamation mark)
035 043 023 00100011 # (number sign)
036 044 024 00100100 $ (dollar sign)
037 045 025 00100101 % (percent)
所以在我的代码示例中,我试图让“$”匹配,但正则表达式的失败:我得到空匹配。但是,如果我使用
std::regex e ("([\\x23-\\x25]+)"); //or
std::regex e ("([\\x23-\\x25\\x21]+)"); //or
std::regex e ("([\\x21\\x24-\\x25]+)"); //or
std::regex e ("([\\x21\\x23-\\x24]+)");
'$'匹配得当,我得到非空匹配结果。
所以我真的不明白这一切的逻辑。你能给我一个提示什么是问题,因为我知道正则表达式组中的范围顺序(例如,a-z)和单个符号(例如,_)[]是不相关的?
我[无法重现该问题(http://coliru.stacked-crooked.com/a/11f0b37c9ea5a007)。 – wally
@Muscampester,你可以在这里试试http://cpp.sh/。问题被转载。 – JenyaKh
那么问题出在'cpp.sh'上。它也适用于[clang](http://coliru.stacked-crooked.com/a/367665a8dae99e9b)和VS2015。 – wally