开发者

boost regular expression capture groups

开发者 https://www.devze.com 2023-03-08 20:33 出处:网络
After a days worth of hacking and reading, I have had no luck with boost\'s regex engine, hopefully someone here can help.

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 ding开发者_StackOverflowo 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!


acording to this articale you have to pass flag match_not_dot_newline to the matching algorithm. i think that would solve your case.

0

精彩评论

暂无评论...
验证码 换一张
取 消