1

In a previous question, I got the answer on a regular expression to accept all email addresses from a certain domain except two from the same domain.

e.g. :-

BAD:
test@testdomain.com
tes2@testdomain.com

GOOD:
notest@testdomain.com
test23@testdomain.com

Here is the regular expression from that answer:

^(?!test@|tes2@)[A-Za-z0-9._%+-]+@testdomain\.com$

However, for my application, I specifically need RE2 regex to be able to use this.

What is the steps I should take to convert this PCRE expression to RE2 type?

  • 4
    Problem is that lookarounds don't seem to be supported by RE2 (see "Empty strings" table [here](https://github.com/google/re2/wiki/Syntax)). I'm afraid this cannot be done in one single regex. Can't you do something like `input.endsWith(@testdomain.com) and !input.startsWith(test@) and !input.startsWith(test2@)`? See also: https://stackoverflow.com/a/30305934/1225328. – sp00m Aug 17 '17 at 11:55
  • 1
    You can convert it to RE2, but it will look long and ugly. I suggest a 2 step approach: 1) if the string matches `^[A-Za-z0-9._%+-]+@testdomain\.com$`, 2) check if it is `test@testdomain.com` or `tes2@testdomain.com`. If yes, fail, else, pass. – Wiktor Stribiżew Aug 17 '17 at 12:59

0 Answers0