Home > Enterprise >  How to require a letter or a number after the special character?
How to require a letter or a number after the special character?

Time:01-17

I am trying to create a regex for specific format of email addresses:

Before the @ sign:

  • Allow only English letters and numbers.
  • Allow dots, dashes and underscores as long as they are not consecutive, and don't allow them to be the last in the string (They have to followed by a letter or a number)

After the @ sign:

  • Allow only single dot
  • TLD has to be minimum 2 letters (currently I also set to 24 maximum but that doesn't matter)

I also allow spaces before and after the entire string (And later trim them)

I managed to get to a working regex that almost matches everything above, except for the part where the special characters (dots, dashes and underscores) have to be followed by a letter or a number.

so this is the regex that works:

/^\s*(?!.*[._-]{2})[\w\.-] @([\w-]) \. [\w-]{2,24}\s*$/

Then I tried adding the part that matches with a letter or a number after the special char but it breaks everything:

(?=[A-Z\d])

So it looks: /^\s*(?!.*[._-]{2})[\w\.-] (?=[A-Z\d]) @([\w-]) \. [\w-]{2,24}\s*$/

But that doesn't work

Test data:

### Should match:
[email protected]
[email protected]
[email protected]
[email protected]
 [email protected]

### Should not match but still matches:
[email protected]

## Should not match and doesn't match (so working correctly):
[email protected]
email@,domain.com

demo: https://regex101.com/r/qYMQlt/1

CodePudding user response:

You could use a negated character class [^\W_] to match word characters without the _

To not match 2 special characters in a row you don't have to use a negative lookahead (?!.*[._-]{2}), but you can repeat matching using a group and start with the "special" character followed by at least 1 or more times other characters.

  • ^ Start of string
  • \s* Match optional whitespace chars
  • [^\W_] Match 1 times a word char without the underscore
  • (?:[._-][^\W_] )* Optionally repeat the same, but allow a single char . _ -
  • @ Match literally
  • [^\W_] Match 1 times a word char without the underscore
  • (?:[_-][^\W_] )* Optionally repeat the same, but allow a single char . _ -
  • \.[\w-]{2,24} Match . and repeat matching 2-24 times either a word char or - (Note that this can also match 24 times a hyphen, perhaps use [a-z]{2,})
  • \s* Match optional whitespace chars
  • $ End of string

Regex demo

  •  Tags:  
  • Related