代码之家  ›  专栏  ›  技术社区  ›  celsowm

这个preg\u replace的Java等价物是什么?

  •  6
  • celsowm  · 技术社区  · 14 年前
    <?php
        $str = "word <a href=\"word\">word</word>word word";
        $str = preg_replace("/word(?!([^<]+)?>)/i","repl",$str);
        echo $str;
        # repl <word word="word">repl</word>
    ?>
    

    资料来源: http://pureform.wordpress.com/2008/01/04/matching-a-word-characters-outside-of-html-tags/

    //谢谢,塞尔索

    3 回复  |  直到 8 年前
        1
  •  12
  •   kolrie    14 年前

    class Test {
      public static void main(String[] args) {
        String str = "word <a href=\"word\">word</word>word word";
        str = str.replaceAll("word(?!([^<]+)?>)", "repl");
        System.out.println(str);
      }
    }
    

    希望这有帮助。

        2
  •  3
  •   Alan Moore Chris Ballance    14 年前

    要将regex翻译成Java语言,您所要做的就是去掉 / i (?i) . 但它不是一个很好的正则表达式;我会用这个来代替:

    (?i)word(?![^<>]++>)
    

    根据RegexBuddy的调试特性,当它尝试匹配 word 在里面 <a href="word">

    str = str.replaceAll("(?i)word(?![^<>]++>)", "repl");
    
        3
  •  1
  •   Zak    14 年前