c++regexbackreference

Regex: Ignoring characters in backreferences


Is it possible to tell std::regex_match to ignore particular character(s) in back references?

For example, I may want to ignore @ and expect "([a-z]*@[a-z]*)\1" to match "foo@barfoobar".

If not (I suspect that it's not possible), what would be another way of solving this problem?


Solution

  • No, there's no way to ignore part of the back-reference. The solution is to split the match into two capture groups so you don't include the unwanted character in the back-reference.

    ([a-z]*)@([a-z]*)\1\2