import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?:[A-Z] \\d|[^\\W\\d_]{2,}\\.?)(?:[- '’][^\\W\\d_]+\\.?)*$";
final String string = "########### Absolutly Fine ################\n"
+ "Mainstreet.\n"
+ "Mainstreet\n"
+ "Main Street\n"
+ "Big New mainstreet\n"
+ "Mainstreet-New\n"
+ "Mains Str.\n"
+ "St. Alexander Street\n"
+ "Übermorgen Straße\n"
+ "John Kennedy Street\n"
+ "Bahnhofstr.\n"
+ "Leonhard-Eck-Str.\n"
+ "Älterweg\n"
+ "Graf-Anton-Weg\n"
+ "Alexanderstraße\n"
+ "Prof.-Ernst-Nathan-Straße\n"
+ "Prof.-Albert-Einstein-Weg\n"
+ "Prof. Ernst-Nathan-Straße\n"
+ "Prof. Albert-Einstein-Weg\n\n\n"
+ "########## Not very common but fine!!! ###################\n"
+ "A 1\n"
+ "K 7\n"
+ "H 2\n"
+ "P 1\n"
+ "I 7\n"
+ "E 7\n"
+ "T 3\n\n\n"
+ "######## Not okay ################\n"
+ "A\n"
+ "B\n"
+ "Mainstreet #+;:_*´`?=)(/&%$§!\n"
+ "Mainstreet#+;:_*´`?=)(/&%$§!\n"
+ "Mainstreet 2\n"
+ "Mainstreet..\n"
+ "Mainstreet§\n\n\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