Reputation: 323
How can I match all the emails listed below through regex:
[email protected]
ma123.23ddy(at)gmail.com
mad12-213dy@gmail(dot)com
ma123ddy[at]gmail[dot]com
mad123dy@gmail[dot]com
maddy[at]gmail.com
rc.joshi62@gov[dot]ab.ws.com
rc.joshi62[at]gov.ab.ws.com
rc.joshi62[at]gov[dot]ab.ws.com
[email protected]
c.123.joshi62(at)gov(dot)ab.ws.com
rc123.jo123shi62(at)gov.ab.ws.com
r123c.joshi62@gov(dot)ab.ws.com
c.123[dot]joshi62(at)gov(dot)ab[dot]ws[dot]com
rc123(dot)jo123shi62(at)gov(dot)ab(dot)ws(dot)com
rc123(dot)jo123shi62[at]gov(dot)ab(dot)ws(dot)com
r123c(dot)joshi62[at]gov(dot)ab[dot]ws[dot]com
I'm trying to match all the complex email id's mentioned in the source code of this site:view-source:https://www.panchayat.gov.in/web/guest/who-s-is-who I'm currently using this Regex as mentioned below:
\b[A-Z0-9._%+-]+@[[A-Z0-9.-]+\.[A-Z]{2,}\b
Upvotes: 0
Views: 905
Reputation: 10930
Ok, here it is: The regex:
\b[A-Z0-9._%+-]+(@|\[at\]|\(at\))[A-Z0-9.-]+((\.|\[dot\]|\(dot\))[A-Z]{2,})+\b
Compared to your own regex, this simply add alternations
(meaning OR).
I add:
(@|\[at\]|\(at\))
which will match an '@
' OR '[at]
' OR '(at)
'.
For dot I add:
(\.|\[dot\]|\(dot\))
which will match a dot
OR '[dot]
' OR '(dot)
'
I then made a group of:
((\.|\[dot\]|\(dot\))[A-Z]{2,})+
which will match things like: '.com' and '.uk.com' one or more times.
Update: I updated the regex to support '.' or '(dot)' or '[dot]' in the name before '@':
\b[A-Z0-9._%+-]+((\.|\[dot\]|\(dot\))[A-Z0-9._%+-]+)*(@|\[at\]|\(at\))[A-Z0-9.-]+((\.|\[dot\]|\(dot\))[A-Z]{2,})+\b
Now it matches all the examples in the question.
BTW: The mistake you made in your recent regex is to include everything in a character group
(between square brackets). That will match every character
in the group, in any order.
Update 2:
Ups, typo, updated the regex.
Upvotes: 1
Reputation: 2670
Given these strings...
[email protected]
ma123.23ddy(at)gmail.com
mad12-213dy@gmail(dot)com
ma123ddy[at]gmail[dot]com
mad123dy@gmail[dot]com
maddy[at]gmail.com
rc.joshi62@gov[dot]ab.ws.com
rc.joshi62[at]gov.ab.ws.com
rc.joshi62[at]gov[dot]ab.ws.com
[email protected]
c.123.joshi62(at)gov(dot)ab.ws.com
rc123.jo123shi62(at)gov.ab.ws.com
r123c.joshi62@gov(dot)ab.ws.com
c.123[dot]joshi62(at)gov(dot)ab[dot]ws[dot]com
rc123(dot)jo123shi62(at)gov(dot)ab(dot)ws(dot)com
rc123(dot)jo123shi62[at]gov(dot)ab(dot)ws(dot)com
r123c(dot)joshi62[at]gov(dot)ab[dot]ws[dot]com
Try...
\b([\w\d]+)?(\.)?([-])?[\w\d]+(\.)?[\w\d]+(@|\[at\]|\(at\))?[\w\d]+(\.|\[dot\]|\(dot\))[\w\d]{2,}(\.[\w\d]+\.[\w\d]+)?(\.[\w\d]+)?\b
UPDATE: Cleaned up version...
\b[\w\d.-]+(@|\[at\]|\(at\))[\w\d]+(\[dot\]|\(dot\)|\.)[\w\d.-]+\b
Upvotes: 0
Reputation: 2670
Given these strings...
[email protected]
maddy[at]gmail[dot]com
maddy@gmail[dot]com
maddy[at]gmail.com
rc.joshi62@gov[dot]ab.ws.com
rc.joshi62[at]gov.ab.ws.com
rc.joshi62[at]gov[dot]ab.ws.com
rc.joshi62.gov.ab.ws.com
c.joshi62(at)gov(dot)ab.ws.com
rc.joshi62(at)gov.ab.ws.com
rc.joshi62@gov(dot)ab.ws.com
The following regex matches the string if it's on its own line...
^([\w\d]+\.)?[\w\d]+(@|\[at\]|\(at\))?[\w\d]+(\.|\[dot\]|\(dot\))[\w\d]{2,}(\.[\w\d]+\.[\w\d]+)?(\.[\w\d]+)?$
If the email appears in a larger string but has spaces (word boundaries before and after) try...
\b([\w\d]+\.)?[\w\d]+(@|\[at\]|\(at\))?[\w\d]+(\.|\[dot\]|\(dot\))[\w\d]{2,}(\.[\w\d]+\.[\w\d]+)?(\.[\w\d]+)?\b
Upvotes: 1