import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "\\s*\n"
+ " (\n"
+ " (?<type>int(?:\\s+long|\\s+short)?|double(?:\\s+long)?|\n"
+ " long(?:\\s+int|\\s+double)?|short(?:\\s+int)?|float|char)\n"
+ " |\n"
+ " (?<error>\\w+|\\w+\\s*\\,)\n"
+ " )\n"
+ " \\s+\n"
+ " (?:\n"
+ " (?:\n"
+ " (?:\n"
+ " (?<error>int|double|float|char|long|short|[^,;a-zA-Z_].+?|[^,;]+?(?:\\s+\\w+?)+)\n"
+ " |\n"
+ " (?<id>[a-zA-Z_][a-zA-Z_0-9]*)\n"
+ " )\n"
+ " (?:\\s*\\[\\s*[1-9]\\d*\\s*\\])*\n"
+ " )\n"
+ " \\s*\n"
+ " (?:\\,\\s*|(?=\\;))\n"
+ " )+\n"
+ "\\s*\\;\\s*";
final String string = "int \n"
+ " abv, cde, doub1lel,ded, cto, int1;\n"
+ "float ng23423, _ga45, \n"
+ "astories; \n"
+ "int af , f;\n"
+ "int a, b, c;int short alt ;\n"
+ "long double fgd [ 1 ], [1] , wef ;\n"
+ "char _a[1], fi [\n"
+ " 3 \n"
+ " ] [ 10] [ 4 ] ,g[1];int qw;";
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