按 | 分割字符串和数字

发布于 2025-01-09 07:51:21 字数 745 浏览 0 评论 0原文

假设我有以下字符串:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

能怎样 2025-01-16 07:51:21

您可以在 \|(?=\d+(?:\||$)) 上进行拆分:

List<String> nums = Arrays.asList(new String[] {
    "123|abc|123abc",
    "123|ab12c|abc|456|abc|def",
    "123|1abc|1abc1|456|abc|wer",
    "123|abc|def|456|ghi|jkl|789|mno|pqr"
});

for (String num : nums) {
    String[] parts = num.split("\\|(?=\\d+(?:\\||$))");
    System.out.println(num + " => " + Arrays.toString(parts));
}

这将打印:

123|abc|123abc => [123|abc|123abc]
123|ab12c|abc|456|abc|def => [123|ab12c|abc, 456|abc|def]
123|1abc|1abc1|456|abc|wer => [123|1abc|1abc1, 456|abc|wer]
123|abc|def|456|ghi|jkl|789|mno|pqr => [123|abc|def, 456|ghi|jkl, 789|mno|pqr]

You may split on \|(?=\d+(?:\||$)):

List<String> nums = Arrays.asList(new String[] {
    "123|abc|123abc",
    "123|ab12c|abc|456|abc|def",
    "123|1abc|1abc1|456|abc|wer",
    "123|abc|def|456|ghi|jkl|789|mno|pqr"
});

for (String num : nums) {
    String[] parts = num.split("\\|(?=\\d+(?:\\||$))");
    System.out.println(num + " => " + Arrays.toString(parts));
}

This prints:

123|abc|123abc => [123|abc|123abc]
123|ab12c|abc|456|abc|def => [123|ab12c|abc, 456|abc|def]
123|1abc|1abc1|456|abc|wer => [123|1abc|1abc1, 456|abc|wer]
123|abc|def|456|ghi|jkl|789|mno|pqr => [123|abc|def, 456|ghi|jkl, 789|mno|pqr]
千里故人稀 2025-01-16 07:51:21

您可以匹配字符串中的各个部分,而不是拆分:

\b\d+(?:\|(?!\d+(?:$|\|))[^|\r\n]+)*
  • \b 单词边界
  • \d+ 匹配 1+ 位数字
  • (?: 非捕获团体
    • \|(?!\d+(?:$|\|)) 匹配 | 并断言不仅是数字,直到下一个管道或管道末尾字符串
    • [^|\r\n]+ 匹配除竖线或换行符之外的 1 个以上字符
  • )* 关闭非捕获组并可选择重复(使用 + 重复一次或多次以匹配至少一个管道char)

正则表达式演示 | Java 演示

String regex = "\\b\\d+(?:\\|(?!\\d+(?:$|\\|))[^|\\r\\n]+)+";
String string = "123|abc|def|456|ghi|jkl|789|mno|pqr";
Pattern pattern = Pattern.compile(regex);
Matcher m = pattern.matcher(string);
List<String> matches = new ArrayList<String>();

while (m.find()) 
    matches.add(m.group());

for (String s : matches)
    System.out.println(s);

输出

123|abc|def
456|ghi|jkl
789|mno|pqr

Instead of splitting, you can match the parts in the string:

\b\d+(?:\|(?!\d+(?:$|\|))[^|\r\n]+)*
  • \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

String regex = "\\b\\d+(?:\\|(?!\\d+(?:$|\\|))[^|\\r\\n]+)+";
String string = "123|abc|def|456|ghi|jkl|789|mno|pqr";
Pattern pattern = Pattern.compile(regex);
Matcher m = pattern.matcher(string);
List<String> matches = new ArrayList<String>();

while (m.find()) 
    matches.add(m.group());

for (String s : matches)
    System.out.println(s);

Output

123|abc|def
456|ghi|jkl
789|mno|pqr
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文