Search code examples
javaregexstringstr-replacereplaceall

Replace anchor tag using regular expression in java?


"TEST START<a class=\"fic.test\" testexpression=\"LTRIM(a)\" testognlexpression=\"${LTRIM(a)}\" href=\"\">a</a>TEST END";

I am having anchor tag in String variable. i want to retrieve only value of attr testognlexpression.

Abouve string should be replaced with this

TEST START ${LTRIM(a)} TEST END

how can i retrieve or replace?

my code looks like

String text = "START<a class=\"fic.test\" testexpression=\"LTRIM(a)\" testognlexpression=\"${LTRIM(a)}\" href=\"\">a</a>END";
    System.out.println(text.replaceAll( "</?a[^>testognlexpression]*>", "" ));
}

Solution

  • text = (text.replaceAll( "(?i)(<a[^>]*?\\stestexpression\\s*)=\"", "" ));
            return text.replaceAll("}\"\\shref.+</a>", "}");
    

    But still looking for 1 line solution .