import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "# Clipping Rectangle\n"
+ "(?<x>\\b[-0-9\\.]+\\b)(\\s)\n"
+ "(?<y>\\b[-0-9\\.]+\\b)(\\s)\n"
+ "(?<width>\\b[-0-9\\.]+\\b)(\\s)\n"
+ "(?<height>\\b[-0-9\\.]+\\b)(\\s)\n"
+ "(re\\nW)\n\n"
+ "(.*?)\n\n"
+ "# Text\n"
+ "(BT)\n"
+ "(?<text>.*?)\n"
+ "(ET)";
final String string = "---\n"
+ "---\n"
+ "85.039 42.52 42.519 42.52 re\n"
+ "W--\n"
+ "---\n"
+ "---\n"
+ "127.559 42.52 42.519 42.52 re\n"
+ "W--\n"
+ "---\n"
+ "---\n"
+ "170.078 42.52 42.52 42.52 re\n"
+ "W--\n"
+ "---\n"
+ "---\n"
+ "BT\n"
+ "---\n"
+ "Text\n"
+ "---\n"
+ "ET\n"
+ "---\n"
+ "---\n"
+ "170.078 42.52 42.52 42.52 re\n"
+ "W--\n"
+ "---\n"
+ "---\n"
+ "127.559 42.52 42.519 42.52 re\n"
+ "W--\n"
+ "---\n"
+ "---\n"
+ "BT\n"
+ "---\n"
+ "Text\n"
+ "---\n"
+ "ET\n"
+ "---\n"
+ "---\n"
+ "170.078 42.52 42.52 42.52 re\n"
+ "W--\n"
+ "---\n"
+ "---\n"
+ "BT\n"
+ "---\n"
+ "Text\n"
+ "---\n"
+ "ET";
final Pattern pattern = Pattern.compile(regex, Pattern.COMMENTS | Pattern.DOTALL);
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