Regex to find the first image in an image tag in an HTML document

前端 未结 3 427
有刺的猬
有刺的猬 2021-01-25 17:03

What is a regex to find the first image in an image tag in an HTML document? My previous tries have not really worked, as they just matched based on .jpg\" and didn

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-25 17:20

    This is a perfect example of a task that is tricky and unreliable with regex, and almost trivially easy with an HTML parser. Use a parser for this, not regex.

    You haven't said which language you're using, but I've heard some very good things about Beautiful Soup, HTML Purifier, and the HTML Agility Pack, which use Python, PHP, and .NET, respectively. Trust me--save yourself some pain and use those instead.

    Edit: If you must use a regex, go with @ridgerunner's pattern.

提交回复
热议问题