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

标签 java regex

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

输入:

"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/

相关文章:

java - 无法使用vertx创建文件

php - 正则表达式替换文件扩展名

javascript - 正则表达式以货币格式替换十进制数和所有句点

正则表达式匹配具有以散列开头并以空格结尾的特殊字符的单词

regex - 如何在正则表达式中使用 awk 变量?

java - Weblogic 通过 Keytool 导入 SSL .PFX 文件

java - 通用方法和具有一个更改方法参数的方法体

java - 选择输出故障

java - 如何在Play 2.0框架中实现 "remember me "功能?

ruby - 如何使用 ruby​​ 正则表达式从标签中提取 href?