.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.7z
类型: 7Z 压缩文件
大小: 6.3 KB