import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(\\d+)\\s?°\\s?(\\d+)\\s?'\\s?(\\d+\\.?\\,?\\d*?)\"\\s?(N|W|S|E)";
final String string = "15° 1' 36.991\" E\n"
+ "15° 2' 21.421\" E\n"
+ "15° 2' 0.712\" E\n"
+ "15° 2' 45.181\" E\n"
+ "15° 3' 1.709\" E\n"
+ "14° 58' 24.822\" E\n"
+ "14° 58' 39.840\" E\n"
+ "14° 58' 26.234\" E\n"
+ "14° 58' 45.145\" E\n"
+ "15° 0' 14.01\" E\n"
+ "15° 0' 11.460\" E\n"
+ "15° 0' 7.928\" E\n"
+ "15° 1' 53.693\" S\n"
+ "15° 2' 24.699\" E\n"
+ "15° 2' 45.547\" E\n"
+ "15° 2' 12.837\" E\n"
+ "15° 2' 43.594\" E\n"
+ "15° 2' 31.644\" E\n"
+ "15° 3' 1.059\" E\n"
+ "14° 59' 46.315\" E\n"
+ "14° 59' 31.694\" E\n"
+ "15° 2' 41.057\" E\n"
+ "14° 58' 40.068\" E\n"
+ "36°57'9\" 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