gpt4 book ai didi

java - 忽略 split() 中的模式

转载 作者:行者123 更新时间:2023-12-02 18:24:22 24 4
gpt4 key购买 nike

我想分割一个由“|”分隔的字符串。但想要忽略带有“|”的字符串值。查找以下示例:

String s = "Shashank|Sam|Location|20246|India|City in India|USA Country|Location|India Specific 2021|25236|A";

我想将Location|India Specific 2021视为一个值。

预期输出:

Shashank
Sam
Location
20246
India
City in India
USA Country
Location|India Specific 2021
25236
A

每当我在字符串中找到 Location|India 时,我想忽略其中的 |

我尝试使用

(?<!Location|India)\|

但是输出是

Shashank
Sam
Location|20246
India
City in India
USA Country
Location|India Specific 2021
25236
A

谢谢

最佳答案

您可以将此正则表达式与负前瞻和嵌套后瞻一起使用:

\|(?!(?<=Location\|)India )

RegEx Demo

正则表达式详细信息:

  • \| :匹配|
  • (?! :开始负向前瞻
    • (?<=Location\|) :确保我们有 Location|在当前位置之前
    • India :匹配India
  • ) :结束负向预测

关于java - 忽略 split() 中的模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70427462/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com