import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(?<forename>.+?)\\s(?<surname>\\w+)\\nBirth\\n(?:(?<birth_day>(\\d{1,2}|unknown))\\s(?<birth_month>\\w{3})\\s(?<birth_year>\\d{4})|\\bunknown\\b)\\n(?<birth_location>.+?\\n)?Death\\n(?:(?<death_day>(\\d{1,2}|unknown))\\s(?<death_month>\\w{3})\\s(?<death_year>\\d{4})(?:\\s*\\(aged\\s*(?<age>\\d+)\\))?|unknown)\\n(?<death_location>.+?)\\nBurial\\n(?<cemetery_name>.+?)\\n(?<cemetery_location>.+?)\\n(?:Show MapGPS-Latitude:\\s*(?<latitude>-?\\d+\\.\\d+),\\s*Longitude:\\s*(?<longitude>-?\\d+\\.\\d+))?\\n?(?:Plot\\n(?<plot>.+?)\\n?)?Memorial ID\\n(?<memorial_id>\\d+)";
final String string = "Frederick Clarke\n"
+ "Birth\n"
+ "6 Feb 1871\n"
+ "Death\n"
+ "7 Nov 1952 (aged 81)\n"
+ "Sheffield\n"
+ "Burial\n"
+ "Crookes Cemetery\n"
+ "Sheffield, Metropolitan Borough of Sheffield, South Yorkshire, England\n"
+ "Show MapGPS-Latitude: 53.384024, Longitude: -1.515043\n"
+ "Plot\n"
+ "MM 7848\n"
+ "Memorial ID\n"
+ "237065233";
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