1

I'm trying to make regex that catch expressions in brackets but when there's more than one section of brackets, regex stop at first closing. An example will explain it better:

(expr)          #simple, it will catch "(expr)"

(expr1(expr2))  #there is bad cuz it catches "(expr1(expr2)"

and I want ". (expr1(expr2))"

I'm currently using this "\\((.*?)\\)" but it doesn't give me what I want.

Bill Tür
  • 2,815
  • 23
  • 33
  • 37
  • 1
    Matching number of brackets can't be expressed in a regular language so this can't be done with regular expressions. That said, you can enable eager catching which will catch the second bracket in your case but will lead to further captures that are not intended. – Hauke Ingmar Schmidt Dec 23 '18 at 13:52
  • (.*) should is good be good – Noman Khan Dec 23 '18 at 14:00

2 Answers2

2

The problem you are trying to solve is generally not suitable for regular expressions, because arbitrarily nested content is not regular text. Instead, you may want to consider writing some sort of parser to find the nested terms. Here is one suggested script:

String input = "Hello (expr1(expr2)) Goodbye (Here is (another (deeply nested))) expression.";
int count = 0;
int start = 0;
int end;
for (int i=0; i < input.length(); ++i) {
    if (input.charAt(i) == '(') {
        ++count;
        if (count == 1) {
            start = i;
        }
    }
    if (input.charAt(i) == ')') {
        --count;
        if (count == 0) {
            System.out.println(input.substring(start, i+1));
        }
    }
}

This works by keeping track of the number of opening and closing parentheses. When we see a ( and the count first goes from zero to one, we record the position in the string where that happened. At the other end, when we see a ) and the count returns to zero, we print the entire (possibly nested) term. In the more general case, you might use a stack data structure to handle the parsing, but a single integer count seems to work here.

The above script outputs the following:

(expr1(expr2))
(Here is (another (deeply nested)))
Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
0

You might use something like this: ^(expresion|expresion2)$

  • ^ asserts position at start of a line
  • $ asserts position at the end of a line
  • | it's used like or operator
  • () to separate the expresions
Leandro Maro
  • 111
  • 8