c#正则表达式常用用法

单个匹配筛选:

Regex reg = new Regex(@"[\s\S]*好评率:[\s\S]*<strong>(\d{0,5}[.]\d{0,5})%</strong>", RegexOptions.IgnoreCase);
Match match = reg.Match(detail);
string year = match.Groups[1].Value;
集合匹配筛选:

string reply = client.DownloadString("http://www.steelinfo.com.cn/web/gqkx/gqkx.asp?choose=%C7%F3");

MatchCollection matchs = new Regex("<td bgcolor=\"#F7F7F7\" width=\"481\" height=\"18\">(.*?)</td>([\\s\\S]*?)<td  bgcolor=\"#F7F7F7\" width=\"481\" height=\"18\"><font color=\"#FF6600\"><b>(.*?)</b></font></td>([\\s\\S]*?)</font><a href=\"(.*?)\" target=\"_blank\"><b><font color=\"#0000FF\">更多>></font></b></a></td>", RegexOptions.IgnoreCase).Matches(reply);
        foreach (Match match in matchs)
        {
            if (DateTime.Compare(DateTime.Now.AddDays(-7), DateTime.Parse(match.Groups[1].Value)) < 0)
            {
                DataRow dr = dt.NewRow();
                dr[0] = match.Groups[3].Value.Replace("……", "").Substring(2); ;
                dr[1] = match.Groups[5].Value;
                dr[2] = DateTime.Parse(match.Groups[1].Value).Month.ToString() + "-" + DateTime.Parse(match.Groups[1].Value).Day.ToString();
                dt.Rows.Add(dr);
            }
        }
是否符合规则:
System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(@"^[+-]?(\d|,*)+(\.)?\d*{1}quot;);
return r.IsMatch(strNumber);
替换:
string r = Regex.Replace(key, @"[^\d.]*", "");
ikmb@163.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值