测试正则表达式

.NET Framework 的 System.Text.RegularExpressions(System.dll)提供正则表达式运算,下面是一个简单的封装,写了一个测试正则表达式的工具。

推荐一篇非常不错的C#正则表达式文:http://www.cnblogs.com/KissKnife/archive/2008/03/23/1118423.html

测试文本摘自JQueryUI的例子:http://docs.jquery.com/UI/Dialog
正则表达式:(http|ftp|https):\/\/[\w]+(.[\w]+)([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])
取网址,摘自博客园的DUDU老大的:http://www.cnblogs.com/dudu/archive/2007/12/19/1006119.html
匹配值中括号值表示:[index:length]

范例代码简摘:

using System; using System.Collections.Generic; using System.Text.RegularExpressions; namespace RegexMatch { /// /// 正则运算 /// public static class RegexUtility { /// /// 判断一个字符串是否匹配某正则表达式 /// /// 输入一个字符串 /// 正则表达式 /// 是否匹配 public static bool RegexMatch(string input, string pattern) { Regex r = new Regex(pattern); Match m = r.Match(input); return m.Success; } /// /// 从一个字符串内得到匹配正则表达式的结果集合 /// /// /// /// public static IList RegexSearch(string input, string pattern) { IList results = new List(); Regex r = new Regex(pattern); MatchCollection mc = r.Matches(input); foreach (Match m in mc) results.Add(string.Format("[{0}:{1}] {2}", m.Index, m.Length, m.Value)); return results; } } }

范例源码下载:http://www.uushare.com/user/m2nlight/file/2713716

RegexMatch.7zRegexMatch.7z
类型: 7Z 压缩文件
大小: 6.3 KB

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值