正则表达式匹配P标签中包含Img标签或单独匹配img标签

本文介绍了在C#开发中如何使用正则表达式来匹配和处理HTML中的img标签,包括替换img标签的alt和title属性。同时,也展示了匹配p标签内包含img标签的正则表达式及其应用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在开发过程中,经常会使用到,使用正则表达式匹配Img标签,具体代码如下:

//正则表达式: <img(.*?)>

//在C#中使用代码实现方法如下

string html = "";   //此处为html代码
Regex regImg = new Regex(@"<img(.*?)>", RegexOptions.IgnoreCase);
                    MatchCollection matches = regImg.Matches(html);
                    if (matches != null && matches.Count > 0)
                    {
                        for (int j = 0; j < matches.Count; j++)
                        {
                            var img = matches[j]?.Value;
                            if (!string.IsNullOrEmpty(img))
                            {
                                var repimg = img;
                                repimg = Regex.Replace(repimg, "alt=\"(.*?)\"", $"alt=\"替换字符串\"", RegexOptions.Compiled);//过滤alt属性
                                repimg = Regex.Replace(repimg, "title=\"(.*?)\"", $"title=\"替换字符串\"", RegexOptions.Compiled);//过滤title属性


                                html = Regex.Replace(html, img, repimg, RegexOptions.Compiled);
                            }
                        }

                    }

使用正则表达式匹配html中 p标签下包含img标签的正则表达式,实现代码如下:

//正则表达式: <p\b[^<>]*>[^<>]*<img\b[^<>]*\/>[^<>]*<\/p>

//C#实现代码如下:

string html = "";   //此处为html代码
Regex regexPArray = new Regex(@"<p\b[^<>]*>[^<>]*<img\b[^<>]*\/>[^<>]*<\/p>", RegexOptions.IgnoreCase);
                MatchCollection matchesValue = regexPArray.Matches(html);
                if (matchesValue != null && matchesValue.Count > 0)
                {
                    for (int i = 0; i < matchesValue.Count; i++)
                    {
                        var pValue = matchesValue[i]?.Value;
                        if (!string.IsNullOrEmpty(pValue))
                        {
                            Regex regImg = new Regex(@"<img(.*?)>", RegexOptions.IgnoreCase);
                            MatchCollection matches = regImg.Matches(pValue);
                            if (matches != null && matches.Count > 0)
                            {
                                for (int j = 0; j < matches.Count; j++)
                                {
                                    var img = matches[j]?.Value;
                                    if (!string.IsNullOrEmpty(img))
                                    {
                                        var repimg = img;
                                        repimg = Regex.Replace(repimg, "alt=\"(.*?)\"", $"alt=\"替换字符串\"", RegexOptions.Compiled);//过滤alt属性
                                        repimg = Regex.Replace(repimg, "title=\"(.*?)\"", $"title=\"替换字符串\"", RegexOptions.Compiled);//过滤title属性

                                        html = Regex.Replace(html, pValue, repimg, RegexOptions.Compiled);
                                    }
                                }

                            }
                        }
                    }
                }

以上就是使用正则表达式匹配img标签或使用正则表达式匹配P标签中包含img的P标签使用方式。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值