5

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!

yggdrasil
  • 95
  • 2
  • 7

1 Answers1

1

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

Ali1S232
  • 3,373
  • 2
  • 27
  • 46
  • Use boost::regex::no_mod_s for perl! It took me a bit of tinkering, but I finally got it to work. You were just a *tad* off, but really close. Because I was using the perl regex engine, it wanted me to use the perl option/version of the flag. (I tried using just match_not_dot_newline, but it was still behaving as it had before). To force the perl engine to set that flag, it looks as though you need to use the boost::regex::no_mod_s flag. Thanks for your help. – yggdrasil May 27 '11 at 14:10
  • that's becouse it was my first encounter to boost and my first try to use regex anywhere other that visual studio search box! – Ali1S232 May 27 '11 at 14:23