import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?P<Username>[-\\w\\.]+)(?:\\s+\\W*at\\W*\\s+|\\s*[^\\@\\s]*?@[^\\@\\s]*?\\s*)(?P<Domain>([\\w\\-]+)(?:(?:\\.|\\s+\\W*?dot\\W*?\\s+)([\\w\\-]+))+?(?:(?:\\.|\\s+\\W*?dot\\W*?\\s+)([\\w\\-]+))??$)";
final String string = "Parse email, also distorted ones, and convert them back to the real ones.\n"
+ "Inspirated by dislick's work: https://regex101.com/r/wB7xJ7/1\n\n"
+ "frenci98@gmail.com\n"
+ "st11389@copernico.bo.it\n"
+ "gigilafontana .at. gmail [dot] com\n"
+ "d98.copernico@gmail.com\n"
+ "domenico-tosello at dotcom-sas dot com\n"
+ "my.e-M4il [at] w3-spAce.org\n"
+ "email@sub-sub.sub dot domain dot info\n"
+ "email@foo dot foo dot foo dot foo";
final String subst = "\\1@\\3.\\4";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
// The substituted value will be contained in the result variable
final String result = matcher.replaceAll(subst);
System.out.println("Substitution result: " + result);
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html