import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<lat0>\\d+)[-|\\s](?<lat1>\\d+)[.|,|\\s](?<lat2>\\d+)['|\\s]?(?<latDir>[N|n|S|s])[,|\\s|\\-|–]+(?<lon0>\\d+)[-|\\s](?<lon1>\\d+)[.|,|\\s](?<lon2>\\d+)['|\\s]?(?<lonDir>[E|e|W|w])";
final String string = "48-22.676N, 004-27.209W\n"
+ "47-14.13'N, 002-16.32'W\n"
+ "48 45,852N - 003 07,217W\n"
+ "44-39.95'N, 001-09.95'W\n"
+ "45-54.35n, 001-19.37w\n"
+ "47-38.542N - 003-14.699W\n"
+ "47-34.39 N – 002-52.41W\n"
+ "47 53 823 N – 3 58 538 W";
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