Arjun
Arjun

Reputation: 1069

Regular Expression to exclude numerical emailids

I have below set of sample emailids

EmailAddress
1123
123.123
123_123
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]

Need to eliminate mailids if they contain entirely numericals before @

Expected output:
[email protected]
[email protected]
[email protected]
[email protected]

I used below Java Rex. But its eliminating everything. I have basic knowledge in writing these expressions. Please help me in correcting below one. Thanks in advance.

[^0-9]*@.*

Upvotes: 0

Views: 79

Answers (3)

collapsar
collapsar

Reputation: 17238

The following regex only lets email adresses pass that meet your specs:

(?m)^.*[^0-9@\r\n].*@

Observe that you have to specify multi-line matching ( m flag. See the live demo. The solution employs the embedded flag syntax m flag. You can also call Pattern.compile with the Pattern.MULTILINE argument. ).

Live demo at regex101.

Explanation

Strategy: Define a basically sound email address as a single-line string containing a @, exclude strictly numerical prefixes.

  • ^: start-of-line anchor
  • @: a basically sound email address must match the at-sign
  • [^...]: before the at sign, one character must neither be a digit nor a CR/LF. @ is also included, the non-digit character tested for must not be the first at-sign !
  • .*: before and after the non-digit tested for, arbitrary strings are permitted ( well, actually they aren't, but true syntactic validation of the email address should probably not happen here and should definitely not be regex based for reasons of reliability and code maintainability ). The strings need to be represented in the pattern, because the pattern is anchored.

Upvotes: 1

l3lackwolf
l3lackwolf

Reputation: 96

do you mean something like this ? (.*[a-zA-Z].*[@]\w*\.\w*)

breakdown
.* = 0 or more characters
[a-zA-Z] = one letter
.* = 0 or more characters
@
\w*\.\w* endless times a-zA-Z0-9 with a single . in between

this way you have the emails that contains at least one letter
see the test at https://regex101.com/r/qV1bU4/3

edited as suggest by ccf with updated breakdown

Upvotes: 1

slugo
slugo

Reputation: 1039

Try this one:

[^\d\s].*@.+

it will match emails that have at least one letter or symbol before the @ sign.

Upvotes: 0

Related Questions