正则表达式:获取无限嵌套括号中的括号

huangapple go评论72阅读模式
英文:

Regex for getting parentheses within parentheses infinitely

问题

所以我有一个文本字符串,假设是"hello world () (foo bar) (foo bar 2 (this looks cozy)) (foo bar 3..."
是否有一个正则表达式模式,我可以使用它来获取括号,并包括其中的任何括号,直到第n层深度。
所以匹配项将是"()""(foo bar)""(foo bar 2 (this looks cozy))"...?

英文:

So I have a String of text lets say "hello world () (foo bar) (foo bar 2 (this looks cozy)) (foo bar 3..."
Is there a regex pattern I could use that will get the parentheses and include any parentheses inside them to nth depth.
So the matches would be "()", "(foo bar)", "(foo bar 2 (this looks cozy))", ...?

答案1

得分: 1

在Java中,正则表达式不支持像其他一些正则表达式引擎那样的递归。相反,您可以编写自己的方法,该方法将仅从字符构建字符串,如果它们是:

  • 在括号内。

要知道当前处理的字符是否在括号内,我们可以创建一个计数器,用于检查括号的平衡(您也可以将其视为嵌套级别的计数器)。简而言之:如果我们看到的多,那么我们就在未关闭的(打开的)括号部分内,因此我们应该将当前字符添加到结果字符串中。

使用这个思路,我们的代码可以如下所示:

String str = "hello world () (foo bar) (foo bar 2 (this looks cozy)) (foo bar 3...)";
List<String> result = new ArrayList<>();

int parenthesisNestingLevel = 0;
StringBuilder sb = new StringBuilder();
for (char ch : str.toCharArray()) {
    if (ch == '(') {
        parenthesisNestingLevel++;
        sb.append(ch);
    } else if (ch == ')') {
        parenthesisNestingLevel--;
        sb.append(ch);
        if (parenthesisNestingLevel == 0) {
            result.add(sb.toString());
            sb.delete(0, sb.length());//reset sb
        }
    } else if (parenthesisNestingLevel > 0) {//we are inside unclosed parenthesis
        sb.append(ch);
    }
}

result.forEach(System.out::println);

输出结果:

()
(foo bar)
(foo bar 2 (this looks cozy))
(foo bar 3...)
英文:

Regex flavor in Java doesn't support recursion like some other flavors do. Instead you can write your own method which will build strings from characters only if they are:

  • (
  • )
  • inside parenthesis.

To know if currently handled character is inside parenthesis we can create counter which will check parenthesis balance (you can also think of it as counter for nesting level). In short: if we saw more ( than ) then we are inside unclosed (open) parenthesis section, so we should add current character to resulting string.

Using that idea our code can look like:

String str = &quot;hello world () (foo bar) (foo bar 2 (this looks cozy)) (foo bar 3...)&quot;;
List&lt;String&gt; result = new ArrayList&lt;&gt;();

int parenthesisNestingLevel = 0;
StringBuilder sb = new StringBuilder();
for (char ch : str.toCharArray()) {
    if (ch == &#39;(&#39;) {
        parenthesisNestingLevel++;
        sb.append(ch);
    } else if (ch == &#39;)&#39;) {
        parenthesisNestingLevel--;
        sb.append(ch);
        if (parenthesisNestingLevel == 0) {
            result.add(sb.toString());
            sb.delete(0, sb.length());//reset sb
        }
    } else if (parenthesisNestingLevel &gt; 0) {//we are inside unclosed parenthesis
        sb.append(ch);
    }
}

result.forEach(System.out::println);

Output:

()
(foo bar)
(foo bar 2 (this looks cozy))
(foo bar 3...)

huangapple
  • 本文由 发表于 2020年8月10日 06:15:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63331852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定