gpt4 book ai didi

java - 如何只替换部分匹配的子串?

转载 作者:行者123 更新时间:2023-12-01 06:01:48 26 4
gpt4 key购买 nike

这是一个我还找不到答案的正则表达式问题:

输入:

"the current time is <start time>00:00:00<end time>. at 00:00:00 there is a firework. Another appearance of 00:00:00."

期望的输出:

"the current time is <start time>00:00:00<end time>. at <start time>00:00:00<end time> there is a firework. Another appearance of <start time>00:00:00<end time>."

解决方案不能涉及首先按句子拆分字符串。

我尝试了什么:

一个简单的 input.replace(group, replace) 将不起作用,因为已经有一个不应该被替换的匹配项。

    public static void main(String[] args) throws ParseException
{
String input = "the current time is <start time>00:00:00<end time>. at 00:00:00 there is a firework. Another appearance of 00:00:00.";
Pattern p = Pattern.compile("(<start time>)?(00:00:00)(<end time>)?");
Matcher m = p.matcher(input);
while(m.find())
{
if(m.group(1) != null) { continue; }
String substr1 = input.substring(0, m.start(2));
String substr2 = input.substring(m.end(2), input.length());
String repl = "<start time>" + m.group(2) + "<end time>";
input = substr1 + repl + substr2;
}
}

最佳答案

您的代码不工作的原因是您在循环内修改 input,使匹配结果的索引无效。

但好消息是您根本不需要循环,您可以结合使用负向后视和负向前视 ( details here ) 来自动跳过已经具有包装器的实例,并使用 replaceAll 来做适合你的循环:

public static void main(String[] args) throws Exception
{
String input = "the current time is <start time>00:00:00<end time>. at 00:00:00 there is a firework. Another appearance of 00:00:00.";
String result = input.replaceAll("(?<!<start time>)00:00:00(?!<end time>)", "<start time>00:00:00<end time>");
// Negative lookbehind -----------^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
// Negative lookahead ------------------------------------/
System.out.println(result);
}

Live Example on IDEone

负向后视表示“如果文本前面有 this,则不匹配”,负向前视表示“如果文本后有 this,则不匹配。”

关于java - 如何只替换部分匹配的子串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47809286/

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