Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost regex sub-string match

Tags:

c++

regex

boost

I want to return output "match" if the pattern "regular" is a sub-string of variable st. Is this possible?

int main()
{
  string st = "some regular expressions are Regxyzr";

  boost::regex ex("[Rr]egular");
  if (boost::regex_match(st, ex)) 
  {
    cout << "match" << endl;
  }
  else 
  {
    cout << "not match" << endl;
  }
}
like image 248
bob Avatar asked Nov 29 '22 06:11

bob


2 Answers

The boost::regex_match only matches the whole string, you probably want boost::regex_search instead.

like image 180
Michael Anderson Avatar answered Dec 01 '22 19:12

Michael Anderson


regex_search does what you want; regex_match is documented as

determines whether a given regular expression matches all of a given character sequence

(the emphasis is in the original URL I'm quoting from).

like image 38
Alex Martelli Avatar answered Dec 01 '22 19:12

Alex Martelli