[英]Split Delimiter java
i've found an answer when searching in stackoverflow, it works perfectly but when i saw the comment the user doesn't explain how the code works, can someone explain to me how does this code means?....
我在 stackoverflow 中搜索时找到了一个答案,它工作得很好,但是当我看到评论时,用户没有解释代码是如何工作的,有人可以向我解释这段代码的含义吗?....
String input =
" [John, 8, 9, 10, \r\n"
+ " , Peter, 10, 8, 7, \r\n"
+ " , Steve, 8, 9, 6, \r\n"
+ " ]";
String output = input.replaceAll("(?m)^[\\s,\\[]+|[\\s,\\]]+$|[\\s&&[^\r\n]]+", "");
System.out.println(output);
Answering your question from the comment:从评论中回答您的问题:
"[\\s&&[^\\r\\n]]+" this the part i dont understand at all, i don't get where the comma disappear while the is nocomma delimiter, and only bracket delimter" – Jansen Stanlie “[\\s&&[^\\r\\n]]+”这是我完全不明白的部分,我不知道逗号在哪里消失,而 is nocomma 分隔符,只有括号分隔符” – Jansen Stanlie
I will ignore the string escapes and write it as true regex.我将忽略字符串转义并将其写为真正的正则表达式。 In the java flavor of regex you can do class intersections and subtractions by using:
在 regex 的 java 风格中,您可以使用以下方法进行类交集和减法:
[..&&[..]] and [..&&[^..] respectively. [..&&[..]]和[..&&[^..]分别。 Replace .. with your character class and inclusion/exclusion range.
将 .. 替换为您的字符类和包含/排除范围。
For instance例如
[az&&[^aeiou]] would give you an english lowercase letter that is not a vowel. [az&&[^aeiou]]会给你一个不是元音的英文小写字母。
So, in your example (removing java string escape) [\\s&&[^\\r\\n]] - \\s any whitespace character, except \\r or \\n.因此,在您的示例中(删除 java 字符串转义) [\\s&&[^\\r\\n]] - \\s 任何空白字符,\\r 或 \\n 除外。 This was done to get rid of all spaces but keep the line breaks.
这样做是为了摆脱所有空格,但保留换行符。
This could also be written as [\\s&&[\\V]] which would make it Unicode compliant.这也可以写为[\\s&&[\\V]] ,这将使其符合 Unicode。 \\V is any character that is NOT a vertical whitespace.
\\V 是不是垂直空格的任何字符。
The question about where the comma is removed: The leading comma is removed here:关于在哪里删除逗号的问题: 此处删除前导逗号:
(?m)^[\\s,\\[]+|[\\s,\\]]+$|[\\s&&[^\r\n]]+
^
and trailing:和尾随:
(?m)^[\\s,\\[]+|[\\s,\\]]+$|[\\s&&[^\r\n]]+
^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.