简体   繁体   中英

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]*>", "" ));
}
       public static void main(String []args){
       String text = "<a class=\"fic.test\" testexpression=\"LTRIM(a)\" testognlexpression=\"${LTRIM(a)}\" href=\"\">a</a>";
       String val=text.replaceAll( ".*testognlexpression=", "" );
       System.out.println(val.split("\\s+")[0].replaceAll("\"",""));
       }

OP :

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

But still looking for 1 line solution .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM