【发布时间】:2015-06-09 13:52:35
【问题描述】:
我想找到[/ 和] 之间的数字(在本例中为12345)。
我写过这样的代码:
float num;
string line = "A111[/12345]";
boost::regex e ("[/([0-9]{5})]");
boost::smatch match;
if (boost::regex_search(line, match, e))
{
std::string s1(match[1].first, match[1].second);
num = boost::lexical_cast<float>(s1); //convert to float
cout << num << endl;
}
但是,我收到此错误:The error occurred while parsing the regular expression fragment: '/([0-9]{5}>>>HERE>>>)]'.
【问题讨论】:
-
你需要转义[
-
你必须避开像
\[和\]这样的外部大括号。 -
是的 - 对不起。
\必须在 c++ 字符串中转义为\\。所以最终的字符串应该看起来像\\[/([0-9]{5})\\] -
@Vera rind 没问题。是的,就可以了。谢谢!