I have a string including e-mail. There are probably extra characters before and / or after it. input examples:
a1@b.com
a2@b.com abcd efg
x y z a3@b.com
p q a4@b.com x z
asd[x5@c.net]gh
I want to remove the extra characters.
Desired outputs:
a1@b.com
a2@b.com
a3@b.com
a4@b.com
x5@c.net
Valid characters are a-zA-Z0-9._ So there are probably invalid characters before and / or after e-mail.
I tried this code to identify whether it is a correct email or not (this assumes that it is separated from extra characters by space), but I can not replace to the desired string (using s.replaceAll()):
if (s.matches("(?i).*\\s[a-zA-Z_\\.]+@[a-zA-Z_\\.]+\\.[a-zA-Z_\\.]+.*") ||
fields[2].matches("(?i).*[a-zA-Z_\\.]+@[a-zA-Z_\\.]+\\.[a-zA-Z_\\.]+\\s.*"))