按 | 分割字符串和数字
假设我有以下字符串:
String one = "123|abc|123abc";
String two = "123|ab12c|abc|456|abc|def";
String three = "123|1abc|1abc1|456|abc|wer";
String four = "123|abc|def|456|ghi|jkl|789|mno|pqr";
如果我对它们进行拆分,我期望得到以下输出:
one = ["123|abc|123abc"];
two = ["123|ab12c|abc", "456|abc|def"];
three = ["123|1abc|1abc1", "456|abc|wer"];
four = ["123|abc|def", "456|ghi|jkl", "789|mno|pqr"];
该字符串具有以下结构:
以 1 个或多个数字开头,后跟随机数(| 后跟随机数字符)。
当|之后它只是数字被认为是一个新值。
更多示例:
In - 123456|xxxxxx|zzzzzzz|xa2314|xzxczxc|1234|qwerty
Out - ["123456|xxxxxx|zzzzzzz|xa2314|xzxczxc", "1234|qwerty"]
尝试了以下多种变体但不起作用:
value.split( "\\|\\d+|\\d+" )
Let's imagine I have the following strings:
String one = "123|abc|123abc";
String two = "123|ab12c|abc|456|abc|def";
String three = "123|1abc|1abc1|456|abc|wer";
String four = "123|abc|def|456|ghi|jkl|789|mno|pqr";
If I do a split on them I expect the following output:
one = ["123|abc|123abc"];
two = ["123|ab12c|abc", "456|abc|def"];
three = ["123|1abc|1abc1", "456|abc|wer"];
four = ["123|abc|def", "456|ghi|jkl", "789|mno|pqr"];
The string has the following structure:
Starts with 1 or more digits followed by a random number of (| followed by random number of characters).
When after a | it's only numbers is considered a new value.
More examples:
In - 123456|xxxxxx|zzzzzzz|xa2314|xzxczxc|1234|qwerty
Out - ["123456|xxxxxx|zzzzzzz|xa2314|xzxczxc", "1234|qwerty"]
Tried multiple variations of the following but does not work:
value.split( "\\|\\d+|\\d+" )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在
\|(?=\d+(?:\||$))
上进行拆分:这将打印:
You may split on
\|(?=\d+(?:\||$))
:This prints:
您可以匹配字符串中的各个部分,而不是拆分:
\b
单词边界\d+
匹配 1+ 位数字(?:
非捕获团体\|(?!\d+(?:$|\|))
匹配|
并断言不仅是数字,直到下一个管道或管道末尾字符串[^|\r\n]+
匹配除竖线或换行符之外的 1 个以上字符)*
关闭非捕获组并可选择重复(使用+ 重复一次或多次以匹配至少一个管道char)
正则表达式演示 | Java 演示
输出
Instead of splitting, you can match the parts in the string:
\b
A word boundary\d+
Match 1+ digits(?:
Non capture group\|(?!\d+(?:$|\|))
Match|
and assert not only digits till either the next pipe or the end of the string[^|\r\n]+
Match 1+ chars other than a pipe or a newline)*
Close the non capture group and optionally repeat (use+
to repeat one or more times to match at least one pipe char)Regex demo | Java demo
Output