从 img 标签中提取 src 属性的正则表达式 [英] Regular Expression to extract src attribute from img tag

查看:58
本文介绍了从 img 标签中提取 src 属性的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一种模式,用于提取 HTML 中 img 标签中找到的文件的路径.

I am trying to write a pattern for extracting the path for files found in img tags in HTML.

String string = "<img src=\"file:/C:/Documents and Settings/elundqvist/My Documents/My Pictures/import dialog step 1.JPG\" border=\"0\" />";

我的模式:

src\\s*=\\s*\"(.+)\"

问题是我的模式还会包含 img 标签的 'border="0" 部分.

Problem is that my pattern will also include the 'border="0" part of the img tag.

什么模式会匹配此文件的 URI 路径而不包含border="0"?

What pattern would match the URI path for this file without including the 'border="0"?

推荐答案

你的模式应该是(未转义的):

Your pattern should be (unescaped):

src\s*=\s*"(.+?)"

重要的部分是添加的问号尽可能少地匹配组

The important part is the added question mark that matches the group as few times as possible

这篇关于从 img 标签中提取 src 属性的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆