正则表达式获取img的src内容

简介: 正则表达式获取img的src内容

如果我们有如下一段数据,我们想获取img中src的内容。

<p>桥边姑娘,我把你放心上</p><p><img src="https://n.sinaimg.cn/news/transform/20171113/puY7-fynship2141885.jpg" _src="https://n.sinaimg.cn/news/transform/20171113/puY7-fynship2141885.jpg" style="width: 550px; height: 317px;"/></p>

具体实现

这里我们分两步走,先匹配img标签,然后再获取src后面的内容。具体代码如下:

private static final Pattern IMAGE_TAG_PATTERN = Pattern.compile("<(img|IMG)(.*?)>");
private static Pattern IMAGE_SRC_PATTERN = Pattern.compile("(src|SRC)=\"(.*?)\"");
private static Pattern IMAGE__SRC_PATTERN = Pattern.compile("(_src|_SRC)=\"(.*?)\"");

public static void matchImgSrcTag(String srcStr) {

    List<String> targets = new ArrayList<>();

    // 针对src标签
    // 先匹配img标签
    Matcher imageTagMatcher = IMAGE_TAG_PATTERN.matcher(srcStr);
    while (imageTagMatcher.find()) {
        String image = imageTagMatcher.group(2).trim();
        // 获取src后面的内容
        Matcher imageSrcMatcher = IMAGE_SRC_PATTERN.matcher(image);
        String src = null;
        if (imageSrcMatcher.find()) {
            src = imageSrcMatcher.group(2).trim();
        }
        if (src == null || src.isEmpty()) {
            continue;
        }
        System.out.println("src:" + src);
        targets.add(src);
    }

    // 针对_src标签
    while (imageTagMatcher.find()) {
        String image = imageTagMatcher.group(2).trim();
        Matcher imageSrcMatcher = IMAGE__SRC_PATTERN.matcher(image);
        String src = null;
        if (imageSrcMatcher.find()) {
            src = imageSrcMatcher.group(2).trim();
        }
        if (src == null || src.isEmpty()) {
            continue;
        }
        System.out.println("_src_:" + src);
        targets.add(src);
    }
}

测试验证

public static void main(String[] args) {
    String src = "<p>桥边姑娘,我把你放心上</p><p><img src=\"https://n.sinaimg.cn/news/transform/20171113/puY7-fynship2141885.jpg\" _src=\"https://n.sinaimg.cn/news/transform/20171113/puY7-fynship2141885.jpg\" style=\"width: 550px; height: 317px;\"/></p>";
    matchImgSrcTag(src);
}

output:

src:https://n.sinaimg.cn/news/transform/20171113/puY7-fynship2141885.jpg
_src_:https://n.sinaimg.cn/news/transform/20171113/puY7-fynship2141885.jpg
相关文章
|
移动开发 C# .NET
C#正则表达式通过HTML提取网页中的图片src
原文:C#正则表达式通过HTML提取网页中的图片src 目前在做HoverTreeCMS项目中有处理图片的部分,参考了一下网上案例,自己写了一个获取内容中的图片地址的方法。可以先看看效果:http://tool.hovertree.com/a/zz/img/  一般来说一个 HTML 文档有很多标签,比如“”、“”、“”等,想把文档中的 img 标签提取出来并不是一件容易的事。
1596 0
正则表达式取&lt;img src=""&gt;src中包含特定字符的地址
string str = @" "; Regex reg = new Regex(@"(?is)]*?src=(['""\s]?)((?:(?!topics)[^'""\s])*)\1[^>]*?>"); foreach (Match m in reg.
1015 0
|
19天前
|
Python
Python 内置正则表达式库re的使用
正则表达式是记录文本规则的代码,用于查找和处理符合特定规则的字符串。在Python中,常通过原生字符串`r&#39;string&#39;`表示。使用`re.compile()`创建正则对象,便于多次使用。匹配字符串有`match()`(从开头匹配)、`search()`(搜索首个匹配)和`findall()`(找所有匹配)。替换字符串用`sub()`,分割字符串则用`split()`。
32 3
|
19天前
|
Python Windows
【Python进阶必备】一文掌握re库:实战正则表达式
【Python进阶必备】一文掌握re库:实战正则表达式
22 0
|
4天前
|
开发者 Python
Python 正则表达式
Python 正则表达式
|
13天前
|
数据安全/隐私保护 Python
Python进阶---正则表达式
Python进阶---正则表达式
12 2
|
17天前
|
数据采集 Python
python中的正则表达式,Python实习面试经验汇总
python中的正则表达式,Python实习面试经验汇总
|
19天前
|
Python
python正则表达式小结
1. **其他函数:**  `re`模块还提供了其他一些函数,例如 `re.search()`查找字符串中第一个匹配的部分,`re.findall()`查找所有匹配的部分,`re.sub()`替换匹配的部分。
25 0
|
19天前
|
Python
使用Python解析网页和正则表达式
使用Python解析网页涉及`requests`和`re`模块。首先导入这两个模块,然后用`requests.get()`发送HTTP请求获取URL内容。通过`.text`属性得到HTML文本。接着,利用正则表达式和`re.search()`匹配特定模式(如网页标题),并用`.group(1)`获取匹配数据。最后,对提取的信息进行处理,如打印标题。实际操作时,需根据需求调整正则表达式。
21 2
|
19天前
|
缓存 运维 前端开发
第十六章 Python正则表达式
第十六章 Python正则表达式