In Python, I am extracting emails from a string like so:
split = re.split(" ", string)
emails = []
pattern = re.compile("^[a-zA-Z0-9_\.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-\.]+$");
for bit in split:
result = pattern.match(bit)
if(result != None):
emails.append(bit)
And this works, as long as there is a space in between the emails. But this might not always be the case. For example:
Hello, foo@foo.com
would return:
foo@foo.com
but, take the following string:
I know my best friend mailto:foo@foo.com!
This would return null. So the question is: how can I make it so that a regex is the delimiter to split? I would want to get
foo@foo.com
in all cases, regardless of punctuation next to it. Is this possible in Python?
By "splitting by regex" I mean that if the program encounters the pattern in a string, it will extract that part and put it into a list.
I’d say you’re looking for
re.findall:Notice that
findallcan handle more than one email address: