import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\d{1,4}[A-Za-z]|\\/?\\d{1,4}";
final String string = "Streets numbers:\n"
+ "/1\n"
+ "78\n"
+ "3a\n"
+ "89/\n"
+ "-1 (special case)\n"
+ "1\n\n"
+ "Apartment:\n"
+ "1-\n"
+ "2-5\n"
+ "12\n"
+ "7a\n"
+ "3a\n\n"
+ "Aadresses for testing:\n"
+ "Põllu tn 3-1-\n"
+ "Joa tn 3a-1-3\n"
+ "Kapa vkt 78\n"
+ "Kivimäe/1\n"
+ "Aia tn 11/2-5\n"
+ "Männiku tee 89/1-438, Nõmme linnaosa, Tallinn, Harju maakond\n"
+ "Männiku pst\n"
+ "Männiku mnt 12345\n"
+ "1. Mai tn\n";
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