C#中正则表达式的高级应用
2012-03-15 21:22
218 查看
1。在正则表达式中定义变量并调用:
using System; using System.Text.RegularExpressions;
publicclass Test {
publicstaticvoid Main () {
// Define a regular expression for repeated words. Regex rx =new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
// Define a test string. string text ="The the quick brown fox fox jumped over the lazy dog dog."; // Find matches. MatchCollection matches = rx.Matches(text);
// Report the number of matches found. Console.WriteLine("{0} matches found.", matches.Count);
// Report on each match. foreach (Match match in matches) { string word = match.Groups["word"].Value; int index = match.Index; Console.WriteLine("{0} repeated at position {1}", word, index); } } }
其中?<word>定义了一个变量,之后的\k<word>调用自身定义的变量word。
2。好用的Regex.Replace 和Match.Result
这个例子实现输入的日期更改格式的功能,用正则表达式自动搜索字符串并替换,注意正则表达式中变量的使用。
publicstring MDYToDMY(string input) { return Regex.Replace(input, "\\b(?<month>\\d{1,2})/(?<day>\\d{1,2})/(?<year>\\d{2,4})\\b", "${day}-${month}-${year}"); }
Match.Result是返回一个可以带正则表达式中变量值的字符串。
publicstring Extension(string url) { Regex r =new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/", RegexOptions.Compiled); return r.Match(url).Result("${proto}${port}"); }
正则式官方详解:
http://msdn.microsoft.com/zh-cn/library/ae5bf541.aspx
using System; using System.Text.RegularExpressions;
publicclass Test {
publicstaticvoid Main () {
// Define a regular expression for repeated words. Regex rx =new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b", RegexOptions.Compiled | RegexOptions.IgnoreCase);
// Define a test string. string text ="The the quick brown fox fox jumped over the lazy dog dog."; // Find matches. MatchCollection matches = rx.Matches(text);
// Report the number of matches found. Console.WriteLine("{0} matches found.", matches.Count);
// Report on each match. foreach (Match match in matches) { string word = match.Groups["word"].Value; int index = match.Index; Console.WriteLine("{0} repeated at position {1}", word, index); } } }
其中?<word>定义了一个变量,之后的\k<word>调用自身定义的变量word。
2。好用的Regex.Replace 和Match.Result
这个例子实现输入的日期更改格式的功能,用正则表达式自动搜索字符串并替换,注意正则表达式中变量的使用。
publicstring MDYToDMY(string input) { return Regex.Replace(input, "\\b(?<month>\\d{1,2})/(?<day>\\d{1,2})/(?<year>\\d{2,4})\\b", "${day}-${month}-${year}"); }
Match.Result是返回一个可以带正则表达式中变量值的字符串。
publicstring Extension(string url) { Regex r =new Regex(@"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/", RegexOptions.Compiled); return r.Match(url).Result("${proto}${port}"); }
正则式官方详解:
http://msdn.microsoft.com/zh-cn/library/ae5bf541.aspx
相关文章推荐
- C#正则表达式(RegEx)高级应用之分组(Group)替换(Replace)
- C#中正则表达式的高级应用
- C#中正则表达式的高级应用
- C#正则表达式(RegEx)高级应用之分组(Group)替换(Replace)
- URLRewrite结合正则表达式(C#)的应用(接上篇)
- c#正则表达式应用实例
- 正则总结:正则表达式在js中的高级应用
- C# 正则表达式应用
- C#高级学习第一章String和正则表达式
- C#中正则表达式在replace中的应用!
- c#正则表达式应用
- C# 利用正则表达式进行忽略大小写的字符串替换的应用--自定义高亮显示
- 正则总结:正则表达式在js中的高级应用
- C# 正则表达式应用积累
- C# 应用正则表达式过滤输入字符串
- C#运用正则表达式智能获取html模版页中模版信息的应用
- C#正则表达式应用范例_C#教程
- [ 转]C# 中使用正则表达式 Regex.Matches方法的几个应用
- 转]c#正则表达式应用实例
- 正则:正则表达式在js中的高级应用