java 如何使用正则表达式根据模式拆分字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15699353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:33:27  来源:igfitidea点击:

How to split a string based on a pattern using regex

javaregex

提问by prasanth

I have trouble splitting string based on regex.

我无法根据正则表达式拆分字符串。

String str = "1=(1-2,3-4),2=2,3=3,4=4";
Pattern commaPattern = Pattern.compile("\([0-9-]+,[0-9-]+\)|(,)") ;
String[] arr = commaPattern.split(str);
for (String s : arr)
{
    System.out.println(s);
}

Expected output,

预期输出,

1=(1-2,3-4)     
2=2    
3=3    
4=4

Actual output,

实际输出,

1=

2=2
3=3
4=4

回答by Anirudha

This regex would splitas required

此正则表达式将根据需要 拆分

,(?![^()]*\))
  ------------
      |->split with , only if it is not within ()

回答by Bart Kiers

This isn't well suited for a split(...). Consider scanning through the input and matching instead:

这不太适合split(...). 考虑扫描输入和matching:

String str = "1=(1-2,3-4),2=2,3=3,4=4";

Matcher m = Pattern.compile("(\d+)=(\d+|\([^)]*\))").matcher(str);

while(m.find()) {
  String key = m.group(1);
  String value = m.group(2);
  System.out.printf("key=%s, value=%s\n", key, value);
}

which would print:

这将打印:

key=1, value=(1-2,3-4)
key=2, value=2
key=3, value=3
key=4, value=4

回答by Hurda

You will have to use some look ahead mechanism here. As I see it you are trying to split it on comma that is not in parenthesis. But your regular expressions says:

您将不得不在这里使用一些前瞻机制。正如我所看到的,您正试图将它拆分为不在括号中的逗号。但是你的正则表达式说:

Split on comma OR on comma between numbers in parenthesis 

So your String gets splitted in 4 places 1) (1-2,3-4) 2-4) comma

所以你的字符串被分成 4 个位置 1) (1-2,3-4) 2-4) 逗号

回答by Achintya Jha

String[] arr = commaPattern.split(str);

should be

应该

String[] arr = str.split(commaPattern);