import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^((struct *[a-z0-9_]+)|[a-z0-9_]+) *[*a-z0-9_]+\\((void|((((struct *[a-z0-9_]+)|[a-z0-9_]+) *[*a-z0-9_]+)(, ((struct *[a-z0-9_]+)|[a-z0-9_]+) *[*a-z0-9_]+)*))\\)";
final String string = "int a_0(void)\n"
+ "int b_1(int a)\n"
+ "int c_2(int a, int b)\n\n"
+ "void *xmalloc(size_t n)\n\n"
+ "int main(int argc, char **argv)\n\n"
+ "void *xmalloc(size_t n)\n\n"
+ "void release(struct s_block)\n\n"
+ "int a_(int a, int b)\n"
+ "int main(void)\n\n"
+ "struct student get_student(char *name)\n"
+ "struct student *grow_2fast(struct student *s, int age_diff)\n"
+ "struct student *grow_2fast(struct student *s, struct hello age_diff)\n"
+ "int main(void)\n\n"
+ "int print_me(int h1)\n\n"
+ "/*\n"
+ "**int main(void)\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