I'm trying to extract a list of function names from a formula, but my regex is not working.
Given ( aaa(111) + bbb(222) ) / ccc ( 333 )
I need to obtain an array of strings containing aaa
, bbb
and ccc
. Instead, I'm getting aaa(
, bbb(
and ccc (
. how to make this work?
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));
}
You've got nested capture groups, make the first one a non-capture group:
Pattern pattern = Pattern.compile("(?:(\\w+)\\s*\\()");
or as pointed out by Didier L just remove the outer group:
Pattern pattern = Pattern.compile("((\\w+)\\s*\\(");