Click here to Skip to main content
14,767,354 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've been trying to write a regex pattern to only match top level domain using bash i.e
person@email.com or person1@email.uk would be valid but person@email.co.uk would be invalid.

What I have tried:

grep -E -o '[a-zA-Z0-9._-]+@[a-zA-Z0-9_-]+\.[a-zA-Z0-9]{2,}' myfile.html

This however still produces the undesired result.
Posted
Updated 25-Nov-20 4:47am
Comments
CHill60 25-Nov-20 9:41am
   
why would "person@email.co.uk" be invalid? Perfectly valid email address, whereas "person1@email.uk" is not. Perhaps that's your problem :laugh:
DrewMc 25-Nov-20 9:46am
   
It is not that it is invalid in the real world. I just don't want it picked up.
CHill60 25-Nov-20 10:40am
   
Fair enough. Was it you that downvoted OriginalGriff's Solution?
Patrice T 25-Nov-20 11:50am
   
Show sample input and output.
and explain in what it is undesired.

1 solution

It's a bad idea.
But ...
^([a-zA-Z0-9_\-\.]+)@[^\.]+\.[^\.]+$
Will do it.
You'll be eliminating a heck of a lot of valid emails though! (And letting bad ones though as well ...)

If you are going to use Regular Expressions, then get a copy of Expresso[^] - it's free, and it examines and generates Regular expressions.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900