【问题标题】:Replace anchor tag using regular expression in java?在java中使用正则表达式替换锚标记?
【发布时间】:2014-05-14 09:59:19
【问题描述】:
"TEST START<a class=\"fic.test\" testexpression=\"LTRIM(a)\" testognlexpression=\"${LTRIM(a)}\" href=\"\">a</a>TEST END";

我在字符串变量中有锚标记。我只想检索 attr testognlexpression 的值。

上面的字符串应该替换成这个

TEST START ${LTRIM(a)} TEST END

如何检索或替换?

我的代码看起来像

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

【问题讨论】:

标签: java regex string str-replace replaceall


【解决方案1】:
       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("\"",""));
       }

操作:

${LTRIM(a)}

【讨论】:

  • 更新了问题:) "TEST STARTa测试结束";
  • 不适合我 :( String text = "TEST aEND"; String text1 = text.replaceAll("*.",""); String text2 = text.replaceAll("\"", ""); System.out.println(text1+text2);
【解决方案2】:
text = (text.replaceAll( "(?i)(<a[^>]*?\\stestexpression\\s*)=\"", "" ));
        return text.replaceAll("}\"\\shref.+</a>", "}");

但仍在寻找 1 线解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多