为什么这不是C++的正则表达式(正则表达式EXP([^,\s] *))?
我在用这句话:为什么这不是C++的正则表达式(正则表达式EXP([^,\s] *))?,c++,regex,C++,Regex,我在用这句话: regex exp ("[^,\s]*"); 和C++编译器正在引发错误。使用来自 i、 e.regex exp sub.*;,我没有得到错误,但通过前面的行,我得到了错误: terminate called after throwing an instance of 'std::regex_error' what(): regex_error 我试图在空格和逗号之间,或者在行尾或行首和空格或逗号之间搜索所有内容 下面是一个脚本,以防您想要测试它: #include
regex exp ("[^,\s]*");
和C++编译器正在引发错误。使用来自
i、 e.regex exp sub.*;,我没有得到错误,但通过前面的行,我得到了错误:
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
我试图在空格和逗号之间,或者在行尾或行首和空格或逗号之间搜索所有内容
下面是一个脚本,以防您想要测试它:
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main()
{
string itemString = "cheese, fries, drink, soda, coke";
regex exp ("[^,\s]*");
//regex exp ("(sub)(.*)");
smatch matches;
regex_match(itemString,matches,exp);
for(int i = 0; i < matches.size();i++){
cout << matches[i] << endl;
}
return 0;
}
不像CPLUS软件。别忘了在用C++代码粘贴时避免ReGEX!@阿德里亚诺你什么意思?我刚刚[^ ]使用了,并且C++ C++反斜杠前缀启动了一个转义序列。在这种情况下\s不是有效的转义序列,则行为是未定义的特定于编译器的。它可以按原样复制、一起删除或替换为s。您可以尝试将其转储到cout,但无论如何,您不应该依赖该行为。您的正则表达式应该是[^,\\s]*。是的,StackOverflow也使用bachslash进行转义;无论如何,你必须修正你的正则表达式。要匹配以逗号分隔的单词列表,只需使用[\w]+