JasonB JasonB - 6 months ago 13
Python Question

python regex get first part of an email address

I am quite new to python and regex and I was wondering how to extract the first part of an email address upto the domain name. So for example if:

s='xjhgjg876896@domain.com'


I would like the regex result to be (taking into account all "sorts" of email ids i.e including numbers etc..):

xjhgjg876896


I get the idea of regex - as in I know I need to scan till "@" and then store the result - but I am unsure how to implement this in python.

Thanks for your time.

Answer

You should just use the split method of strings:

s.split("@")[0]
Comments