0

Need regex which will detect first.second@domain.tld

but not detect on ([a-z0-9A-Z])@domain.tld

valid: abc123XYZ@domain.tld

invalid: abc12.de456@domain.tld

I recognize a period is legal preceeding @ in an email address but need to detect that condition.

CSᵠ
  • 10,049
  • 9
  • 41
  • 64

3 Answers3

2

Assuming that you've already validated this as a proper email address:

\..*@

Which means: A ., then a sequence of 0 or more characters, then a @

jonvuri
  • 5,738
  • 3
  • 24
  • 32
1

You don't need regular expressions for this, just compare the position of the period against the at symbol:

// assuming there's already an `@` in $email
if (($p = strpos($email, '.')) !== false && $p < strpos($email, '@')) {
    echo 'invalid';
}

If there's no @ in the email address, it will not print 'invalid' because any integer will not strictly be smaller than false.

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
0

\.(?=.*@) Should do the trick.

Daedalus
  • 1,667
  • 10
  • 12
  • I'm curious why you'd use a lookahead here. – jonvuri Mar 11 '13 at 22:07
  • I used look-ahead to only capture the "." instances coming before the "@", but without actually including the "@" in my match – Daedalus Mar 11 '13 at 22:08
  • This regex is only being used to detect a true/false match, from what I understand, so there is no need to exclude the `@` – jonvuri Mar 11 '13 at 22:10
  • Maybe they highlight the problem character for the user after validation. I don't know, just how I chose to write the answer :) – Daedalus Mar 11 '13 at 22:15