I once had to find out if a string contains a call to a Java method (including method names containing non-ASCII characters).
It worked pretty well for me, although it also finds constructor calls. Hope this helps.
public static final Pattern PARENTHESES_REGEX = Pattern .compile("(?U)([.\\w]+)\\s*\\((.*)\\)"); private static List<String> keyWordsBeforeParens = Arrays.asList("while", "for", "if", "try", "catch", "switch"); private static boolean containsMethodCall(final String s) { final Matcher matcher = PARENTHESES_REGEX.matcher(s); while (matcher.find()) { final String beforeParens = matcher.group(1); final String insideParens = matcher.group(2); if (keyWordsBeforeParens.contains(beforeParens)) { System.out.println("Keyword: " + beforeParens); return containsMethodCall(insideParens); } else { System.out.println("Method name: " + beforeParens); return true; } } return false; }
source share