我想拆分此表达式
A+ + B
之间的“+”让我拥有 A +和B在最后
请注意,A之后的+是第一个令牌的一部分,我不想拆分它
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <boost/algorithm/string/regex.hpp>
#include <vector>
using namespace std;
int main()
{
string expression="A+ + B";
vector <string> resultArray;
boost::algorithm::split_regex( resultArray, expression, boost::regex( " + " ));
for (int i=0; i<resultArray.size();i++){
cout <<resultArray[i]<< endl ;
}
return 0;
}
答案 0 :(得分:1)
+
是一个正则表达式字符,你必须逃避它。不确定它是如何在c ++中完成的,但通常在其他语言中使用反斜杠(\)来完成,例如\+
,对于空格,你可以使用\s
所以假设这将是你的分裂正则表达式:
\\s+\\+\\s+
它意味着:任意数量的空格,然后是加号,然后是任意数量的空格。
答案 1 :(得分:0)
+
需要使用\
进行转义,此外\
本身需要再次使用\
进行转义,因为这是C ++。所以:
int main()
{
string expression="A+ + B";
vector <string> resultArray;
boost::regex rx(" \\+ ");
boost::algorithm::split_regex( resultArray, expression, rx);
for (size_t i=0; i<resultArray.size();i++)
{
cout << "[" << i << "] : " << resultArray[i]<< endl ;
}
return 0;
}
输出:
jdibling@hurricane:~/dev/hacks$ ./hacks
[0] : A+
[1] : B