通过代码学习C#&.NET——委托使用(正则表达式替换)
来源:互联网 发布:阿里云 mysql 优化 编辑:程序博客网 时间:2024/05/29 04:23
代码编写及运行环境Visual Studio 2010 .NET v4.0.30319
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;namespace DelegateUseInRegexReplace{ /// <summary> /// 有的时候通过正则表达式替换字符串中匹配内容中部分内容,需要使用委托实现之。 /// 本例实现的是在html源代码转换可能出现的一种情况,即需要对超链接中href属性中的域名或IP进行整体的替换 /// 本例中是把IP地址192.168.1.23替换为202.145.65.15 /// 正则表达式替换中的委托MatchEvaluator匹配的是具有一个Match参数返回string的方法 /// </summary> class Program { static void Main(string[] args) { string html = "<br /><a href=\"http://192.168.1.23/index.html\">192.168.1.23/index.html</a>"; Console.WriteLine("原始字符串:"); Console.WriteLine(html); string htmlResult = Regex.Replace(html, "<a[^<]*>", new MatchEvaluator(ReplaceIP)); Console.WriteLine("替换后字符串:"); Console.WriteLine(htmlResult); } public static string ReplaceIP(Match match) { return match.Value.Replace("192.168.1.23", "202.145.65.15"); } }}
运行结果为:
- 通过代码学习C#&.NET——委托使用(正则表达式替换)
- 通过代码学习C#&.NET——委托使用(线程、任务)
- 通过代码学习C#&.NET——委托使用(事件、定时处理)
- 通过代码学习C#&.NET——委托使用(通用类设计)
- 通过代码学习C#&.NET——委托使用(通用类设计)
- 通过代码学习C#&.NET——委托基础
- eclipse使用—正则表达式匹配替换
- .Net 正则表达式替换
- Myeclipse正则表达式替换代码
- NET正则表达式使用高级技巧之替换类
- VS.net IDE中,使用正则表达式进行查找、替换
- .NET正则表达式使用高级技巧之替换类
- xcode中使用正则表达式来搜索替换代码
- 正则表达式在.Net中的使用(C#)
- 正则表达式在.Net中的使用(C#)
- 正则表达式在.Net中的使用(C#)
- C#.net正则表达式使用集锦
- c#.net如何使用正则表达式
- [经典]Linux内核中ioremap映射的透彻理解
- 删除自身的文件
- 主题:unicode中文范围
- QT显示中文
- Nginx+Php5-fpm在Ubuntu10.04中的安装配置方法
- 通过代码学习C#&.NET——委托使用(正则表达式替换)
- 浅析QStringr的Unicode存储特性(二)
- googleADK开发环境的搭建
- BMP文件结构
- 每天一个C程序
- BSON格式解释
- 嵌入式系统 Boot Loader 技术内幕
- 更新DataTable的方法
- URL处理两个关键的函数parse_str与http_build_query