簡體   English   中英

如何只替換一些匹配的子串?

[英]How to replace only some of the matched substrings?

這是一個我無法找到答案的正則表達式問題:

輸入:

"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 ,使匹配結果上的索引無效。

但好消息是你根本不需要循環,你可以使用負向lookbehind和負向前瞻( 這里的詳細信息 )的組合來跳過已經自動包裝的實例,並使用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);
}

IDEone上的實例

負面的背后說“如果文字在它前面就不匹配”,否定的前瞻說“如果文字在它之后就不匹配”。

Lookahead和lookbehind斷言可以幫助你。

負面觀察"(?<!start)text"匹配"footext"但不匹配"starttext"

否定前瞻"text(?!end)"匹配"textfoo"但不匹配"textend"

將此應用於您的案例會導致: "(?<!<start time>)(00:00:00)(?!<end time>)"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM