-3

I have the sentences "This is the best day of my life" and "Here are the best
day
of my life"and "best day". I wish to match "best" followed by "day" using regex, i.e the sentence should contain the expression "best day".Note that there can be any number of spaces between "best" and "day", however, day should come after best.

I wrote this regex - (best)(\\s+)(day) and used regex_match in C++ but it's not working. I'm not very familiar with Regex; could somebody guide me?

bool isValidBestDay(string str)
{
    string bestDay = "(best)(\\s+)(day)";
    regex bestDayMatch(suchThat, ECMAScript | icase);
    return regex_match(str, bestDayMatch);
}

The problem I'm facing on C++ is that if I have a string 'best day' it matches, but not if I have something like "this best day". Thanks!

2 Answers2

1

Use regex_search instead of regex_match, since:

regex_search will successfully match any subsequence of the given sequence, whereas std::regex_match will only return true if the regular expression matches the entire sequence.

djhaskin987
  • 9,741
  • 4
  • 50
  • 86
-2

The regex you're looking for is

((best)\s*(day))

Goto this website http://regexr.com

In the Expression box put the regex I gave and In the Text box put following examples, you'll understand.

ex.

This is the best day of my life

This is the best day of my life

This is the best not day of my life

Ankush Rathi
  • 622
  • 1
  • 6
  • 26
  • Great now try your regex against `My name is bestday`, does it match? Also, you should try to present the input/output in a more legible way. What will it catch, what will it not catch? – ctwheels Sep 07 '17 at 20:01
  • `\s+` is fine and prefered in this case, there is another issue in OPs code – Slava Sep 07 '17 at 20:21