Regex substring fitting

I want to return the result of "match" if the "regular" pattern is a substring of the variable st. Is it 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;
  }
}
+5
source share
3 answers

The boost :: regex_match value matches only the entire line, you might need boost :: regex_search.

+16
source

regex_search does what you want; regex_match is documented as

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

(the emphasis is on the source URL, which I quote).

+7
source

- boost:: regex

Alternative approach:

You can use boost :: regex_iterator, this is useful for parsing a file, etc.

string[0], 
string[1] 

below indicates the start and end iterator.

Example:

boost::regex_iterator stIter(string[0], string[end], regExpression)
boost::regex_iterator endIter

for (stIter; stIter != endIter; ++stIter)
{
   cout << " Whole string " << (*stIter)[0] << endl;
   cout << " First sub-group " << (*stIter)[1] << endl;
}

}

0
source

All Articles