0

From email address like

xxx@site.co.uk
xxx@site.uk
xxx@site.me.uk

I want to write a regex which should return 'uk' is all the cases.

I have tried

'+@([^.]+)\..+' 

which gives only the domain name. I have tried using

'[^/.]+$'  

but it is giving error.

Jerry
  • 70,495
  • 13
  • 100
  • 144
amitbisai
  • 133
  • 3
  • 9

4 Answers4

3

The regex to extract what you are asking for is:

\.([^.\n\s]*)$  with /gm modifiers

explanation:

    \. matches the character . literally
1st Capturing group ([^.\n\s]*)
    [^.\n\s]* match a single character not present in the list below
        Quantifier: Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
        . the literal character .
        \n matches a fine-feed (newline) character (ASCII 10)
        \s match any white space character [\r\n\t\f ]
$ assert position at end of a line
m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
g modifier: global. All matches 

for your input example, it will be:

import re
m = re.compile(r'\.([^.\n\s]*)$', re.M)                                             
f = re.findall(m, data)                                                             
print f 

output:

['uk', 'uk', 'uk']

hope this helps.

Ammar
  • 1,305
  • 2
  • 11
  • 16
2

As myemail@com is a valid address, you can use:

@.*([^.]+)$
Toto
  • 89,455
  • 62
  • 89
  • 125
2

You don't need regex. This would always give you 'uk' in your examples:

>>> url = 'foo@site.co.uk'
>>> url.split('.')[-1]
'uk'
alan
  • 4,752
  • 21
  • 30
0

Simply .*\.(\w+) won't help?

Can add more validations for "@" to the regular expression if needed.

Jeroen
  • 60,696
  • 40
  • 206
  • 339
Murli
  • 728
  • 5
  • 15