如何使用 Java 正则表达式拆分此字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3881184/
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-30 03:49:41  来源:igfitidea点击:

How to split this string using Java Regular Expressions

javaregex

提问by Arun P Johny

I want to split the string

我想拆分字符串

String fields = "name[Employee Name], employeeno[Employee No], dob[Date of Birth], joindate[Date of Joining]";

to

name
employeeno
dob
joindate

I wrote the following java code for this but it is printing only name other matches are not printing.

我为此编写了以下 java 代码,但它仅打印名称,其他匹配项未打印。

String fields = "name[Employee Name], employeeno[Employee No], dob[Date of Birth], joindate[Date of Joining]";

Pattern pattern = Pattern.compile("\[.+\]+?,?\s*" );

String[] split = pattern.split(fields);
for (String string : split) {
    System.out.println(string);
}

What am I doing wrong here?

我在这里做错了什么?

Thank you

谢谢

回答by Bart Kiers

This part:

这部分:

\[.+\]

matches the first [, the .+then gobbles up the entire string (if no line breaks are in the string) and then the \\]will match the last ].

匹配第一个[.+然后吞噬整个字符串(如果字符串中没有换行符),然后\\]将匹配最后一个]

You need to make the .+reluctant by placing a ?after it:

您需要.+通过?在它后面放置一个来使不情愿:

Pattern pattern = Pattern.compile("\[.+?\]+?,?\s*");

And shouldn't \\]+?just be \\]?

不应该\\]+?只是\\]

回答by Mark Byers

The error is that you are matching greedily. You can change it to a non-greedy match:

错误是您正在贪婪地匹配。您可以将其更改为非贪婪匹配:

Pattern.compile("\[.+?\],?\s*")
                      ^

回答by Philipp Jardas

There's an online regular expression tester at http://gskinner.com/RegExr/?2sa45that will help you a lot when you try to understand regular expressions and how they are applied to a given input.

http://gskinner.com/RegExr/?2sa45上有一个在线正则表达式测试器,当您尝试理解正则表达式以及它们如何应用于给定输入时,它将对您有很大帮助。

回答by r0ng

WOuld it be better to use Negated Character Classesto match the square brackets? \[(\w+\s)+\w+[^\]]\]

使用否定字符类来匹配方括号会更好吗?\[(\w+\s)+\w+[^\]]\]

You could also see a good example how does using a negated character class work internally (without backtracking)?

您还可以看到一个很好的例子,在内部使用否定字符类是如何工作的(没有回溯)?