Common下RegHelper.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Common
{
    public class RegHelper
    {
        //返回单个路径

        public static string GetImgUrl(string HTMLStr)
        {
            string str = string.Empty;
            //string sPattern = @"^<img/s+[^>]*>";
            Regex r = new Regex(@"<img/s+[^>]*/s*src/s*=/s*([']?)(?<url>/S+)'?[^>]*>",
            RegexOptions.Compiled);
            Match m = r.Match(HTMLStr.ToLower());
            if (m.Success)
                str = m.Result("${url}");
            return str;
        }
        //返回多个路径的情况
        public static StringBuilder MyGetImgUrl(string text)
        {
            StringBuilder str = new StringBuilder();
            string pat = @"<img/s+[^>]*/s*src/s*=/s*([']?)(?<url>/S+)'?[^>]*>";

            Regex r = new Regex(pat, RegexOptions.Compiled);

            Match m = r.Match(text.ToLower());
            //int matchCount = 0;
            while (m.Success)
            {
                Group g = m.Groups[2];
                str.Append(g).Append(",");
                m = m.NextMatch();
            }
            return str;
        }
        /// <summary>
        /// 根据id过滤div内容
        /// </summary>
        /// <param name="html"></param>
        /// <param name="id">id</param>
        /// <param name="include_div_mask">是否包含div标记</param>
        /// <returns></returns>
        public static string GetDivByID(string html, string id, bool include_div_mask)
        {
            string regex_pattern = string.Format(include_div_mask ? @"(?is)<div[^>]*?id=""{0}.+?</div>" : @"(?is)(?<=<div[^>]*?id=""{0}[^>]+>).+?(?=</div>)", Regex.Escape(id));
            Match m = Regex.Match(html, regex_pattern);
            if (m.Success) return m.Value;
            else return "";
        }


    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值