Search code examples
javaphpregexreplaceall

What is the Java equivalent to this preg_replace?


<?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>
?>

source: http://pureform.wordpress.com/2008/01/04/matching-a-word-characters-outside-of-html-tags/

Unfortunality my project needs a semantic libs avaliable only for Java...

// Thanks Celso


Solution

  • Use the String.replaceAll() method:

    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);
      }
    }
    

    Hope this helps.