3

I am using boost::xpressive to parse through my text file. I want to see if only when the line begins with a '#' (one of more times).

I am using the following code

std::string str1 = "## this could be my line";
sregex rex1 = sregex::compile("^#+");
smatch result1;
if(regex_match(str1,result1,rex1){
    std::cout << result1[0] << '\n';
}else{
std::cout << "match not found!\n";
}

But I am always getting "match not found!", even when the lines begin with #. Can anyone help me here?

Incidently, can anyone also help me in writing the rex1 statement using the boost::xpressive 'bos'?

Thanks! Ayesha

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Ayesha Kalra
  • 173
  • 3
  • 12

1 Answers1

3

Use regex_search instead of regex_match.

And here is the static xpressive syntax for rex1:

bos >> +as_xpr('#');
ildjarn
  • 62,044
  • 9
  • 127
  • 211
  • Wow! Thanks a lot!, it worked !! Can you explain why regex_match won't match? I thought '##' should match '^#+'. – Ayesha Kalra Apr 11 '12 at 21:26
  • @Ayesha : `"##"` would indeed match `"^#+"`, but `"## this could be my line"` (as in your question's code) would not. Quoting [the docs](http://www.boost.org/doc/html/xpressive/user_s_guide.html#boost_xpressive.user_s_guide.quick_start), "*For regex_match() to succeed, the whole string must match the regex, from beginning to end.*" So your regex would have to be `"^#+.*"` in order for `regex_match` to work with your question's input. Hint: read the docs. ;-] – ildjarn Apr 11 '12 at 21:30
  • Got it!! Thanks a lot. Got to re-read the docs more carefully! – Ayesha Kalra Apr 11 '12 at 21:35