import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(\n"
+ " (\\d+|pi|\\\\u03C0|\\\\u03A0|phi|\\\\u03C6|\\\\u03A6)\n"
+ " |\n"
+ " (abs|acos|cos|atan|tan|asin|sin|ceil|floor|sqrt|log|ln)\\([^)]+\\)\n"
+ ")\n"
+ "(\n"
+ " \\s*([+\\-*\\/^]|mod)\\s*\n"
+ " (\n"
+ " (\\d+|pi|\\\\u03C0|\\\\u03A0|phi|\\\\u03C6|\\\\u03A6)\n"
+ " |\n"
+ " (abs|acos|cos|atan|tan|asin|sin|ceil|floor|sqrt|log|ln)\\([^)]+\\)\n"
+ " )\n"
+ ")*\n"
+ "\\s*=";
final String string = "test text (1) pi=\n"
+ "test text (1 pi=\n"
+ "test text 1) pi=\n"
+ "test text (2) pi + 10 / 20=\n"
+ "test text (3) test pi ^ 10 / 20=\n"
+ "test text (30) 10 + 5=\n"
+ "test text (500) abs(10 + 5)=\n"
+ "test text (1) pi + 10 / 20=\n"
+ "test text 10*5=\n"
+ "test text pi / phi=\n"
+ "test text 10 mod phi=\n"
+ "test text 50 10 mod phi=\n"
+ "test text pi mod abs(phi)=\n"
+ "apple banana cherry apple 10 apple cherry banana hi 10 99+1=\n";
final Pattern pattern = Pattern.compile(regex, 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