Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost regular expression capture groups

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!

like image 948
yggdrasil Avatar asked Nov 14 '22 22:11

yggdrasil


1 Answers

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

like image 164
Ali1S232 Avatar answered Dec 22 '22 07:12

Ali1S232