java 用任意数量的逗号和空格拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15280994/
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
Split a string with arbitrary number of commas and spaces
提问by Aboutblank
I have a String that I'm trying to turn into a list but I get empty entries.
我有一个字符串,我想把它变成一个列表,但我得到了空条目。
",A,B,C,D, ,,,"
returns
[, A, B, C, D, , , ,]
I want to remove all "empty" commas:
我想删除所有“空”逗号:
[A, B, C, D]
I'm trying
我正在努力
current.split(",+\s?")
which does not produce the result I want. What regex should I use instead?
这不会产生我想要的结果。我应该使用什么正则表达式?
回答by Bohemian
You need two steps, but only one line:
您需要两个步骤,但只有一行:
String[] values = input.replaceAll("^[,\s]+", "").split("[,\s]+");
The call to replaceAll()
removes leading separators.
The split is done on any number of separators.
replaceAll()
删除前导分隔符的调用。
拆分是在任意数量的分隔符上完成的。
The behaviour of split()
means that a trailing blank value is ignored, so no need to trim trailing separators before splitting.
的行为split()
意味着忽略尾随空白值,因此无需在拆分前修剪尾随分隔符。
Here's a test:
这是一个测试:
public static void main(String[] args) throws Exception {
String input = ",A,B,C,D, ,,,";
String[] values = input.replaceAll("^[,\s]+", "").split("[,\s]+");
System.out.println(Arrays.toString(values));
}
Output:
输出:
[A, B, C, D]
回答by Bergi
You do not only want to include the next few whitespaces into your match, but also the consecutive commata to split on them as one unit:
您不仅要在匹配中包含接下来的几个空格,还要将连续的逗号作为一个单元拆分:
(,\s*)+
current.split("(?:,\s*)+")
回答by Simon Nickerson
I would use Splitter in Guavafor this:
Splitter.on(',').omitEmptyStrings().trimResults().split(",A,B,C,D, ,,,");
as I find this easier to read than the regex.
因为我发现这比正则表达式更容易阅读。
回答by samjewell
Matching any chars otherthan commas and spaces is likely to be a cleaner solution:
匹配任何字符其他比逗号和空格很可能是一个清洁的解决方案:
/[^, ]+/g
/[^, ]+/g
",A,B,C,D, ,,,".match(/[^, ]+/g)
// → ["A", "B", "C", "D"]
If you're working in Javascript you could also use the Lodash _.words
method (kudos to them for the above regex):
如果您使用的是 Javascript,您还可以使用 Lodash_.words
方法(他们对上述正则表达式表示敬意):
_.words('fred, barney, & pebbles', /[^, ]+/g);
// → ['fred', 'barney', '&', 'pebbles']