how can I exctract attribute value using JAVA regex

前端 未结 2 1572
长情又很酷
长情又很酷 2021-01-20 16:36

I have such string:



        
相关标签:
2条回答
  • 2021-01-20 17:16
    myString.replaceFirst(myString, "^<a\\s+href\\s*=\\s*\"([^\"]+)\".*", , "$1");
    

    assuming myString contains your string with the a element.

    As the href attributes cannot be nested, this should be fine and no full HTML parser is needed. A restriction is that it will only find href attributes in double quotes.

    0 讨论(0)
  • 2021-01-20 17:31

    For this particular string you can try something like

    Pattern pattern = Pattern.compile("<a\\shref=\"([^\"]+)");
    //or if you cant use group numbers use look-behind mechanism like
    //Pattern.compile("(?<=<a\\shref=\")[^\"]+");
    Matcher matcher = pattern.matcher(yourString);
    if (matcher.find())
        System.out.println(matcher.group(1));
    

    but if your string can change (like href atrubute can have other atributes before it) it can not work as expected. That is one of the reasons to use parsers rather then regex.

    0 讨论(0)
提交回复
热议问题