import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?<!\\\\) # negative look-behind to make sure start is not escaped \n"
+ "(?: # start non-capture group for all possible match starts\n"
+ " # group 1, match dollar signs only \n"
+ " # single or double dollar sign enforced by look-arounds\n"
+ " ((?<!\\$)\\${1,2}(?!\\$))|\n"
+ " # group 2, match escaped parenthesis\n"
+ " (\\\\\\()|\n"
+ " # group 3, match escaped bracket\n"
+ " (\\\\\\[)| \n"
+ " # group 4, match begin equation\n"
+ " (\\\\begin\\{equation\\})\n"
+ ")\n"
+ "# if group 1 was start\n"
+ "(?(1)\n"
+ " # non greedy match everything in between\n"
+ " # group 1 matches do not support recursion\n"
+ " (.*?)(?<!\\\\)\n"
+ " # match ending double or single dollar signs\n"
+ " (?<!\\$)\\1(?!\\$)| \n"
+ "# else\n"
+ "(?:\n"
+ " # greedily and recursively match everything in between\n"
+ " # groups 2, 3 and 4 support recursion\n"
+ " (.*(?R)?.*)(?<!\\\\)\n"
+ " (?:\n"
+ " # if group 2 was start, escaped parenthesis is end\n"
+ " (?(2)\\\\\\)| \n"
+ " # if group 3 was start, escaped bracket is end\n"
+ " (?(3)\\\\\\]| \n"
+ " # else group 4 was start, match end equation\n"
+ " \\\\end\\{equation\\}\n"
+ " )\n"
+ "))))";
final String string = "NOTE: For test purposes each test has been contained to one line, please enable the s flag for multi-line matches.\n\n"
+ "$ match $ don't match nested $ match $\n\n"
+ "$$ no match$\n"
+ "$no match$$\n"
+ "\\\\$no match\\\\$\n"
+ "$no match\\\\$\n"
+ "\\\\$no match\\\\$\n\n"
+ "$$ match $$ don't match nested $$ match $$\n\n"
+ "$$$ no match $$\n"
+ "$$ no match $$$\n"
+ "\\\\$$ no match$$\n"
+ "$$ no match\\\\$$\n"
+ "\\\\$$ no match\\\\$$\n\n"
+ "\\( match \\)\n"
+ "\\( 1 \\( nested match \\) 3 \\)\n\n"
+ "\\\\( no match \\\\)\n"
+ "\\( no match \\\\\\\\)\n"
+ "\\\\\\\\( no match \\\\\\\\)\n\n"
+ "\\[ match \\]\n"
+ "\\[ 1 \\[ nested match \\] 3 \\]\n\n"
+ "\\\\\\\\[ no match \\\\]\n"
+ "\\\\[ no match \\\\\\\\]\n"
+ "\\\\\\\\[ no match \\\\\\\\]\n\n"
+ "\\begin{equation} match \\end{equation}\n"
+ "\\begin{equation} 1 \\begin{equation} nested match \\end{equation} \\end{equation}\n\n"
+ "\\\\\\\\begin{equation} no match \\\\end{equation}\n"
+ "\\\\begin{equation} no match \\\\\\\\end{equation}\n"
+ "\\\\\\\\begin{equation} no match \\\\end{equation}";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.COMMENTS);
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