我要匹配的图案的表达C++正则表达式匹配“+”量词
的空间,随后通过(加法运算或减法运算符)
例如: " +"
应返回True
我曾尝试使用std::regex_match
以下常规EXP的:
" [+-]"
,"\\s[+-]"
,"\\s[+\\-]"
,"\\s[\\+-]"
但他们都返回false。
什么应该是正确的表达?
编辑
下面是测试代码:
#include<iostream>
#include<string>
#include<regex>
using std::cout;
int main()
{
std::string input;
std::cin>>input;
const std::regex ex(" [\\+-]");
std::smatch m;
if(std::regex_match(input,ex))
{
cout<<"\nTrue";
}
else
cout<<"\nFalse";
return 0;
}
您使用regex_match,或regex_search? –
正如我所提到的。这是'regex_match' – vivek
对不起,我的错误。所以你匹配的字符串只能是“+”或“ - ”?由于regex_match不在字符串中搜索,因此只能检查整个字符串是否与指定的正则表达式匹配。如果你想匹配一个字符串的一部分,使用regex_search –