简体   繁体   中英

How can I use regular expression to grab an 'img' tag?

I want to grab an img tag from text returned from JSON data like that. I want to grab this from a string:

<img class="img" src="https://fbcdn-photos-c-a.akamaihd.net/hphotos-ak-frc3/1239478_598075296936250_1910331324_s.jpg" alt="" />

What is the regular expression I must use to match it?

I used the following, but it is not working.

"<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>"

您可以简单地使用此表达式来匹配img标记,如示例中所示:

<img([\w\W]+?)/>

Your regex doesn't match the string, because it's missing the closing / .

Edit - No, the / is not necessary, so your regex should have worked. But you can relax it a bit like below.

Slightly modified:

 <img\s[^>]*?src\s*=\s*['\"]([^'\"]*?)['\"][^>]*?>

Please note you shouldn't use regular expressions to parse HTML for the various reasons

<img\s+[^>]*src="([^"]*)"[^>]*>

Or use Jsoup ...

String html = "<img class=\"img\" src=\"https://fbcdn-photos-c-a.akamaihd.net/
               hphotos-ak-frc3/1239478_598075296936250_1910331324_s.jpg\" alt=\"\" />";

Document doc = Jsoup.parse(html);
Element img = doc.select("img").first();
String src = img.attr("src");

System.out.println(src);

I face the same situation and I tried this and it worked for me.

(<img)[^/>]*(/>|>)

Here is the explanation:

图片用于解释上述正则表达式

This explanation is from the website https://extendsclass.com/regex-tester.html

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