我要匹配的圖案的表達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 –