import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "((RESERVADAS|[A-Z]+)[(][)][ ]*[\\t]*[\\r\\n|\\n]*([\\{][\\r\\n|\\n]*([\\t]*[ ]*[0-9]+[ ]*[=][ ]*(['][A-Z]+['])[\\r\\n|\\n]*)+[\\r\\n|\\n]*[\\}][\\r\\n|\\n]*)*)";
final String string = "RESERVADAS() \n"
+ "{\n"
+ " 18 = 'PROGRAM'\n"
+ " 19 = 'INCLUDE'\n"
+ " 20 = 'CONST'\n"
+ " 21 = 'TYPE'\n"
+ " 22 = 'VAR'\n"
+ " 23 = 'RECORD'\n"
+ " 24 = 'ARRAY'\n"
+ " 25 = 'OF'\n"
+ " 26 = 'PROCEDURE'\n"
+ " 27 = 'FUNCTION'\n"
+ " 28 = 'IF'\n"
+ " 29 = 'THEN'\n"
+ " 30 = 'ELSE'\n"
+ " 31 = 'FOR'\n"
+ " 32 = 'TO'\n"
+ " 33 = 'WHILE'\n"
+ " 34 = 'DO'\n"
+ " 35 = 'EXIT'\n"
+ " 36 = 'END'\n"
+ " 37 = 'CASE'\n"
+ " 38 = 'BREAK'\n"
+ " 39 = 'DOWNTO'\n"
+ "} \n"
+ "COSO()\n"
+ "{\n"
+ " 19='A'\n"
+ "}\n\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