4

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;
  }
}
2
  • Have you tried it? If so, what happened? Commented Jan 14, 2010 at 3:03
  • yes, I get "not match" if st = "regular" I get "match" Commented Jan 14, 2010 at 3:13

3 Answers 3

15

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

Sign up to request clarification or add additional context in comments.

Comments

7

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).

Comments

0

Your question is answered with example in library documentation - boost::regex

Alternate approach:

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

string[0], 
string[1] 

below indicates start and end iterator.

Ex:

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;
}

}

1 Comment

The accepted answer on this page better explains sregex_iterator stackoverflow.com/questions/2593288/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.