Regular expression to get an attribute from HTML tag

前端 未结 4 1708
难免孤独
难免孤独 2020-12-03 03:55

I am looking for a regular expression that can get me src (case insensitive) tag from following HTML snippets in java.



        
4条回答
  •  日久生厌
    2020-12-03 04:02

    This answer is for google searchers, Because it's too late

    Copying cletus's showed error and Modifying his answer and passing modified String src\\s*=\\s*([\"'])?([^\"']*) as parameter passed into Pattern.compile worked for me,

    Here is the full example

        String htmlString = "
    "; //Sample HTML String ptr= "src\\s*=\\s*([\"'])?([^\"']*)"; Pattern p = Pattern.compile(ptr); Matcher m = p.matcher(htmlString); if (m.find()) { String src = m.group(2); //Result }

提交回复
热议问题