0

I have below set of sample emailids

EmailAddress
1123
123.123
123_123
123@123.123
123@123.com
123@abc.com
123mbc@abc.com
123mbc@123abc.com
123mbc@123abc.123com
123mbc123@cc123abc.c123com

Need to eliminate mailids if they contain entirely numericals before @

Expected output:
123mbc@abc.com
123mbc@123abc.com
123mbc@123abc.123com
123mbc123@cc123abc.c123com

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]*@.*
Arjun
  • 1,049
  • 6
  • 23
  • 37

3 Answers3

1

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.
collapsar
  • 17,010
  • 4
  • 35
  • 61
  • Thank you for the quick answer. This expression marked everything invalid – Arjun Feb 25 '16 at 15:30
  • 1
    Have you checked the live demo with the updated version ? – collapsar Feb 25 '16 at 15:31
  • yes, I tired updated one. Still its marking all as invalid. I am using this exp on java engine. Pentaho ETL tool. – Arjun Feb 25 '16 at 15:37
  • 1
    You have to compile the regex pattern in your java code along these lines : `Pattern p = Pattern.compile(pattern1, Pattern.MULTILINE);`. For details, see [the original SO answer](http://stackoverflow.com/a/3652392/). – collapsar Feb 25 '16 at 15:44
1

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

l3lackwolf
  • 96
  • 4
  • 1
    The regex works in OP's scenario. But If emails contain (.) or (-), your regex won't work. e.g, emails like: john-smith@example.com or john.smith@example.com. In this case, should use: `(.*[a-zA-Z].*@\w*.\w*)`. – Quinn Feb 25 '16 at 15:39
  • @ccf this expression worked as expected. Many thanks. Can you please explain this expression. – Arjun Feb 25 '16 at 15:45
  • @Gokul: The dot(.) matches any single character except new line. Please check this out: https://regex101.com/r/oO6oP6/2. – Quinn Feb 25 '16 at 15:52
  • 1
    The solution will fail for prefixes tothe at-sign not containing letters,e.g. `123-456@abc.com`, `123_456@abc.com`; see live demo on [regexx101](https://regex101.com/r/mO2aF9/1). – collapsar Feb 25 '16 at 15:55
  • @collapsar rex given by "@ccf" didn't break. It marked your e.g as invalid in my code. – Arjun Feb 25 '16 at 16:07
  • @l3lackwolf: I think this is the best approach. Instead of looking for only digits as I did, this rex looks for at least on character before @. Great! – Arjun Feb 25 '16 at 16:19
0

Try this one:

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

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

slugo
  • 1,019
  • 2
  • 11
  • 22