Using a python script I want to exclude all characters at and before the '@' from the userlist*. I only want to see the full domain names. I have tried achieving this using regex, replace, substrings, custom functions etc... but nothing is generating the output I need. I feel like I'm looking In the wrong direction and I must be missing something simple.
* Userlist:
user@domain.com
anotheruser@somedomain.org
superuser@domains.co.uk
foo@domain.com
email = 'user@domain.com'
_, domain = email.split('@')
print domain
>>> domain.com
For completeness, here is (a) solution using regular expressions:
>>> import re
>>> re.search(r'(?<=@).*', 'me@example.com').group()
'example.com'
As an alternative to split() you can slice the index as follows
email = 'user@domain.com'
domain = email[email.index['@']+1:]
print domain
>>> domain.com
This is a slightly safer version of @chishaku's answer; it returns everything following the first occurrence of the target character or substring, and will not choke on 0 or multiple occurrences.
def after_first(ch, s):
return s.split(ch, 1)[-1]
for user in userlist:
print after_first("@", user)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.