gpt4 book ai didi

Java拆分和替换

转载 作者:行者123 更新时间:2023-12-01 07:50:23 25 4
gpt4 key购买 nike

我有以下字符串

String path = "(tags:homepage).(tags:project mindshare).(tags:5.5.x).(tags:project 5.x)";

我使用了以下代码,

String delims = "\\.";

String[] tokens = path.split(delims);
int tokenCount = tokens.length;
for (int j = 0; j < tokenCount; j++) {
System.out.println("Split Output: "+ tokens[j]);
}

当前输出为

Split Output: (tags:homepage)
Split Output: (tags:project mindshare)
Split Output: (tags:5
Split Output: 5
Split Output: x)
Split Output: (tags:project 5
Split Output: x)

最终目标是将这个字符串转换为

String newpath = "(tags:homepage)(tags:project mindshare)(tags:5.5.x)(tags:project 5.x)";

最佳答案

您可以使用正向回顾来实现这一点:

String path = "(tags:homepage).(tags:project mindshare).(tags:5.5.x).(tags:project 5.x)";
String newPath = path.replaceAll("(?<=\\))\\.", ""); // look for periods preceeded by `)`
System.out.println(newPath);

操作:

(tags:homepage)(tags:project mindshare)(tags:5.5.x)(tags:project 5.x)

关于Java拆分和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38825369/

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