boost 正则表达式捕获组

发布于 2024-11-10 09:58:21 字数 1159 浏览 0 评论 0原文

经过一天的黑客和阅读后,我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海不蓝 2024-11-17 09:58:21

根据这篇文章 您必须将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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文