0

I have an existing email address validation Regex and am looking for an additional Regex function that will also NOT match if the sample ends with .ocm.
The reason for this request is to provide additional data entry validation to prevent the most common typo we encounter: .ocm instead of .com.

For example, blah@somewhere.com should return a match, but blah@somewhere.ocm should not.

I'm afraid I don't know what flavor of Regex I'm using, as it's in a black box proprietary application that "accepts Regex" for user input validation. I do know that the application is probably written in VB6 if that helps at all.

Here is the existing Regex, which works well for its intended purpose:


^(?=[a-zA-Z0-9][a-zA-Z0-9@._%+-]{5,253}$)[a-zA-Z0-9@._%+-]{1,64}@(?:(?=[a-zA-Z0-9-]{1,63}\.)[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+)*\.){1,8}[a-zA-Z]{2,63}$

The sample will always be a greater-than-zero-length string, so it doesn't matter if these aren't handled.

It seems that a negative lookbehind at the end of the Regex should be the best method, but I can't find the correct syntax to make this work with my existing Regex. Here are some methods that might work if someone could kindly suggest how to integrate them with my existing Regex:

https://stackoverflow.com/a/406408/6195684

https://code.i-harness.com/en/q/fa3887

https://stackoverflow.com/a/11432373/6195684

Thanks kindly in advance.

HamishKL
  • 111
  • 2

1 Answers1

2

Just add a negative lookahead as shown below:

(?!.*[.]ocm$)

This is the original regex.

This is the modified regex.

Gurmanjot Singh
  • 10,224
  • 2
  • 19
  • 43
  • Thank you very much, this works perfectly. I had mistakenly assumed that this condition needed to be at the end of the regex since that's where the unwanted suffix is. Greatly appreciated indeed. I have a lot to learn about regex syntax. – HamishKL Sep 05 '18 at 06:11
  • 1
    Glad that it helped you. You can practice regex in the provided links. Other great sites - [RexEgg](https://www.rexegg.com/) and [Regex-Info](https://www.regular-expressions.info/) – Gurmanjot Singh Sep 05 '18 at 06:36
  • Thank you. I actually sourced most of the original regex from Regex-Info, and I spent 3 hours trying different syntax on Regex101 before posting here. I think I just had my sequencing screwed up. Experience is everything. – HamishKL Sep 05 '18 at 19:09