import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\d+(\\.\\d+)?([Ee]\\D+)?";
final String string = "This is just a test\n"
+ "where we can have multiple email addresses\n"
+ "which can be gil@home.com\n"
+ "g.echeverria@tec.mx\n"
+ "gil2@one.long.domain.org\n"
+ "Or something more elaborate\n"
+ "one1.two2-three3@is.this-site.real.net\n"
+ "brave_world@survive.com.it\n"
+ "And just like that there can me more formats for emails\n"
+ "But how can we extract those\n"
+ "34.76e4\n"
+ "875\n"
+ "34.43.34.676";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
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