正则表达式用于在忽略括号的情况下分割字符串:

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

regex for splitting a string while ignoring the brackets

问题

Sure, here's the translated content:

我想要通过空格字符来分割一个字符串,但忽略括号之间的空格。我使用的代码是:

String s = "hello (split this) string";
String reg = "(?<=([^\\(].*))( )(?=(.*[^\\)]))";
System.out.println(Arrays.toString(s.split(reg)));

我期望的输出是:

[hello , (split this) , string]

但我得到了这个错误:

Exception in thread "main" java.util.regex.PatternSyntaxException: Look-behind group does not have an obvious maximum length near index 12
(?<=([^(].))z(?=(.[^)]))

我需要一个正则表达式来获得期望的输出。所以,请有人帮忙。

英文:

I want to split a string with space-character but ignoring the space between two brackets.
The code i used is:

String s = &quot;hello (split this) string&quot;;
String reg = &quot;(?&lt;=([^\\(].*))( )(?=(.*[^\\)]))&quot;;
System.out.println(Arrays.toString(s.split(reg));

My expected output is:

[hello , (split this) , string]  

but i get this error
>Exception in thread "main" java.util.regex.PatternSyntaxException: Look-behind group does not have an obvious maximum length near index 12
(?<=([^(].
))z(?=(.*[^)])) *

I need a regex to get the expected output.
So, somebody please help.

答案1

得分: 1

你可以使用以下正则表达式来实现你的需求:

[ ](?=[^\)]*?(?:\(|$))

以上正则表达式的解释:
>[ ] - 表示一个空格字符。
>
> (?=[^\)]*?(?:\(|$)) - 表示正向先行断言,断言括号()内的所有内容。
>
>(?:) - 表示一个非捕获组
>
> | - 表示或操作。
>
> $ - 表示字符串的结尾。

你可以在这里找到上述正则表达式的演示。

正则表达式用于在忽略括号的情况下分割字符串:

在Java中的实现

import java.util.Arrays;
public class Main
{
    public static void main(String[] args) {
        String s = "hello (split this) string";
        String reg = "[ ](?=[^\\)]*?(?:\\(|$))";
        System.out.println(Arrays.toString(s.split(reg)));
    }
}
// 输出: [hello, (split this), string]

你可以在这里找到上述实现。

英文:

You can use the below regex to achieve your requirement:

[ ](?=[^\)]*?(?:\(|$))

Explanation of the above regex:
>[ ] - Represents a space character.
>
> (?=[^\)]*?(?:\(|$)) - Represents a positive look-ahead asserting everything inside of ().
>
>(?:) - Represents a non-capturing group.
>
> | - Represents alternation.
>
> $ - Represents the end of the test String.

You can find the demo of the above regex in here.

正则表达式用于在忽略括号的情况下分割字符串:

IMPLEMENTATION IN JAVA

import java.util.Arrays;
public class Main
{
    public static void main(String[] args) {
        String s = &quot;hello (split this) string&quot;;
        String reg = &quot;[ ](?=[^\\)]*?(?:\\(|$))&quot;;
        System.out.println(Arrays.toString(s.split(reg)));
    }
}

// output: [hello, (split this), string]

You can find the above implementation here.

答案2

得分: 1

尝试使用 &quot;(?:[^ (]+|(?&gt;\\([^()]*\\))|\\()+(?=[ ]|$)&quot;

注意:

  • 用于匹配所有元素,而不是进行分割
  • 将匹配不平衡的括号,并在括号上使用原子组,如 ( kk hh )

示例

英文:

Try &quot;(?:[^ (]+|(?&gt;\\([^()]*\\))|\\()+(?=[ ]|$)&quot;

notes

  • use to match all elements instead of split
  • will match unbalanced parens and use a atomic group on paren like ( kk hh )

demo

huangapple
  • 本文由 发表于 2020年5月31日 00:25:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/62105411.html
匿名

发表评论

匿名网友

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

确定