boost 正则表达式捕获组
经过一天的黑客和阅读后,我对 boost 的正则表达式引擎没有运气,希望这里有人可以提供帮助。
我想从最后一个字段与某些输入匹配的每一行中获取第一个字段。
string input =
"449 a dingo ate my baby THING\n"
"448 a dingo ate my baby THING\n"
"445 a dingo ate my baby BOOGNISH\n"
"446 a dingo ate my baby BOOGNISH\n"
"447 a dingo ate my baby STUFF\n";
假设我给我的正则表达式以下字符串...
string re = "^([0-9]+).+?boognish$";
boost::regex expression(re,boost::regex::perl | boost:regex::icase);
然后设置我的匹配
const int subs[] = { 0, 1 };
boost::sregex_token_iterator it(input.begin(), input.end(), expression, subs);
boost::sregex_token_iterator end;
while ( it != end )
{
fprintf(stderr,"%s|\n", it->str().c_str());
*it++;
}
这是我从 boost 获得的输出,请记住我要求整行和第 1 组匹配,我还要求“|”所以我们可以很容易地看到该行的结尾:
449 a dingo ate my baby THING
448 a dingo ate my baby THING
445 a dingo ate my baby BOOGNISH|
449|
446 a dingo ate my baby BOOGNISH|
446|
我真的想要 445|和 446|只是,但它给了我 449(直到它遇到第一个 BOOGNISH),然后是 446。我已经在其他重新解析器上测试了这个,它似乎工作正常。我在提升方面做错了什么?
先感谢您!
After a days worth of hacking and reading, I have had no luck with boost's regex engine, hopefully someone here can help.
I want to grab the first field out of every line where the last field matching some input.
string input =
"449 a dingo ate my baby THING\n"
"448 a dingo ate my baby THING\n"
"445 a dingo ate my baby BOOGNISH\n"
"446 a dingo ate my baby BOOGNISH\n"
"447 a dingo ate my baby STUFF\n";
Let's say I give my regex the the following string...
string re = "^([0-9]+).+?boognish$";
boost::regex expression(re,boost::regex::perl | boost:regex::icase);
and then set up my match
const int subs[] = { 0, 1 };
boost::sregex_token_iterator it(input.begin(), input.end(), expression, subs);
boost::sregex_token_iterator end;
while ( it != end )
{
fprintf(stderr,"%s|\n", it->str().c_str());
*it++;
}
Here is the output I'm getting from boost, keep in mind I asked for both the entire line and group 1 match, I also asked for a "|" so we can easily see the end of the line:
449 a dingo ate my baby THING
448 a dingo ate my baby THING
445 a dingo ate my baby BOOGNISH|
449|
446 a dingo ate my baby BOOGNISH|
446|
I really want 445| and 446| only, but it's giving me 449 (until it hits the first BOOGNISH) and then 446. I've tested this on other re parsers, and it seems to work fine. What am I doing wrong with boost?
Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据这篇文章 您必须将
flag match_not_dot_newline
传递给匹配算法。我认为这会解决你的案子。acording to this articale you have to pass
flag match_not_dot_newline
to the matching algorithm. i think that would solve your case.