3

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));
    }
Didier L
  • 18,905
  • 10
  • 61
  • 103
ps0604
  • 1,227
  • 23
  • 133
  • 330

2 Answers2

2

You have 2 capturing groups in your pattern:

  • the external parentheses
  • the ones around \\w+

Since you are only interested in the second one, you should either

  • take the second group: matcher.group(2)
  • remove the external parentheses:

    Pattern pattern = Pattern.compile("(\\w+)\\s*\\(");
    

Notice that matcher.group(0) is always the match of the whole pattern (so equivalent to your external parentheses here)

Didier L
  • 18,905
  • 10
  • 61
  • 103
0

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*\\(");
Community
  • 1
  • 1
adamdc78
  • 1,153
  • 8
  • 18