I have a string including e-mail. There are probably extra characters before and / or after it. input examples:
[email protected]
[email protected] abcd efg
x y z [email protected]
p q [email protected] x z
asd[[email protected]]gh
I want to remove the extra characters.
Desired outputs:
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
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.*"))