Java regex: extraction function names
I am trying to extract a list of function names from a formula, but my regex is not working.
Considering ( aaa(111) + bbb(222) ) / ccc ( 333 )
I need to get an array of strings containing aaa
, bbb
and ccc
. Instead, I get aaa(
, bbb(
and ccc (
. how to do it?
This is my attempt:
String formula = "( aaa(111) + bbb(222) ) / ccc ( 333 )";
Pattern pattern = Pattern.compile("((\\w+)\\s*\\()");
Matcher matcher = pattern.matcher(formula);
while(matcher.find()){
System.out.println(matcher.group(1));
}
source to share
There are 2 capture groups in your template:
- outer brackets
- those around
\\w+
Since you are only interested in the second, you must either
- take the second group:
matcher.group(2)
-
remove the outer brackets:
Pattern pattern = Pattern.compile("(\\w+)\\s*\\(");
Note that it matcher.group(0)
always matches the whole pattern (which is the equivalent of your outer brackets here)
source to share