1

See the code below:

const std::string line = "myemail@gmail.com";
const std::string egrep = ".*gmail.*";
std::regex        txt_regex(egrep.c_str());

if (!std::regex_match(line, txt_regex)) {
    std::cout << "Not matched?!" << std::endl;
}
else {
    std::cout << "Matched" << std::endl;
}

It will print out Matched. However, if I end that line with CR or LF (\r or \n respectively) it will print Not matched?!

Is that a bug or expected reaction on CR with default regex flags?

Grzegorz
  • 3,207
  • 3
  • 20
  • 43
  • `.` does not match line break chars and `regex_match` requires the full string match, so `string\n` won't match. Use `regex_search` to find a partial match or replace `.` with `[\s\S]` / `[^]`. – Wiktor Stribiżew Mar 01 '19 at 00:08
  • 1
    This seems to have been hastily closed. However, I don't feel it was entirely justified. The proper answer to what you are asking centers on the fact that the `.` wildcard matches any character _except_ newline. Newline is considered either CR or LF, so if you want to allow those in a full match, then use `".*gmail.*[\r\n]*"`. It should be noted though, that these are strictly not valid characters for an email address, so depending on what you're using this for, you may first want to _sanitize your inputs_. Alternatively, a `regex_search` with the pattern `"gmail"` would achieve the same. – paddy Mar 01 '19 at 00:11
  • The dupes look appropriate to me. Don't know what's too hasty when pointing someone to the answer. – Lightness Races in Orbit Mar 01 '19 at 00:32
  • I disagree it should be closed as the examples of the answers are not matching the case. These examples are much wider, so would require different type of a question when looking for an answer to this specific case. – Grzegorz Mar 01 '19 at 16:09

0 Answers0