[转]Using Replacement Strings with Regex.Replace
本文转自:http://www.knowdotnet.com/articles/regereplacementstrings.html
The String.Replace function has been a valuable tool for years, allowing programmers to change strings by replacing a specific substring with another substring. While this is usually enough, the Regex.Replace function goes a step (ok, maybe steps) further. It allows replacement of text using regular expressions. Not only can you define the text to replace using a regular expression, you can define what to replace it with using a replacement string containing special constructs which identify portions of the mathed text. A common example is the representation of a name. Let's say you have a name in a string like "John Doe", but you would really like to have "Doe, John". You could accomplish this with a few lines of code, splitting the string on a space and constructing a new string using the elements, or you could do it in one simple line:
strInput = Regex.Replace(strInput,"(?<first>\S+) (?<last>\S+)","${last},${first}")
This is a simplified example with a simple expression (we don't allow names like John J. Doe, John Boy Doe, John Doe, Jr., etc.), but it shows how using the Regex.Replace function can take several lines of code down to just a few. If we were to write a more complex expression, it would still require just one line of code in our application, whereas we may have to add dozens of lines of code with If...Else blocks or Select Case statements to accomplish complicated parsing.
Here's a look at some of the constructs that can be used in a Regex replacement string:
$& - matched text
$_ - original source string
$` - text before match
$' - text after match
${group_name} - text matched by named group
$, $ - text matched by numbered group
$$ - the literal "$"
Here are several examples showing how the Regex.Replace method can perform useful operations in code:
VB
'Convert tab characters into 4 spaces
sInput = Regex.Replace(sInput,"\t"," ")
C#
'Convert tab characters into 4 spaces
sInput = Regex.Replace(sInput,"\t"," ");
VB
'Put $ in front of monetary values
sResult = Regex.Replace("The price is 31.95","\d+\.\d{2}","$$$&")
C#
'Put $ in front of monetary values
sInput = Regex.Replace(sInput,"\d+\.\d{2}","$$$&");
[转]Using Replacement Strings with Regex.Replace的更多相关文章
- Regex.Replace的基本用法
Regex构造函数Regex(string pattern)Regex(string pattern,RegexOptions options)参数说明pattern:要匹配的正则表达式模式optio ...
- 正则表达式之Regex.Replace()用法
正则表达式替换匹配到的字符串 string txt = "AAA12345678AAAA"; //匹配到的连续数字的前4位用*替换 string m =Regex.Replace( ...
- vb.net 使用 Regex Replace 正则 替换 Html字串的table中tbody第一个tr下的td为th
本次示例效果如下: TextBox1中输入如下字符串: 12<table><tbody><tr><td>1<br/>11</td> ...
- C#正则表达式replace用法
Regex构造函数Regex(string pattern)Regex(string pattern,RegexOptions options)参数说明pattern:要匹配的正则表达式模式optio ...
- Inside TSQL Querying - Chapter 3. Query Tuning
Tuning Methodology When dealing with performance problems, database professionals tend to focus on t ...
- JavaScript常用表单验证正则表达式(身份证、电话号码、邮编、日期、IP等)
身份证正则表达式 //身份证正则表达式(15位)isIDCard1=/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;//身份证正则表达式 ...
- Inside Microsoft SQL Server 2008: T-SQL Querying 读书笔记之查询优化
一. 自顶向下优化方法论 1. 分析实例级别的等待 在实例级找出什么类型的等待占用大部分的时间,通过sys.dm_os_wait_stats select wait_type, --等待类型 wait ...
- SQL点滴15—在SQL Server 2008中调用C#程序
原文:SQL点滴15-在SQL Server 2008中调用C#程序 T-SQL的在执行普通的查询的时候是很高效的,但是在执行循环,判断这样的语句的时候效率就不那么的高了.这时可以借助CLR了,我们可 ...
- 在SQL Server 2008中调用.net,dll
原文:在SQL Server 2008中调用.net,dll T-SQL的在执行普通的查询的时候是很高效的,但是在执行循环,判断这样的语句的时候效率就不那么的高了.这时可以借助CLR了,我们可以在SQ ...
随机推荐
- [VS2012]无法新建或者编译已有的项目
今天启动VS2012时,发现提示插件错误,然后打开以前的网站时,发现报错如下: ContractNameMicrosoft.VisualStudio.Utilities.IContentTypereg ...
- urllib2中自定义opener
正常用Python抓取网页信息,需要用到urllib2,调用urllib2.urlopen(url),可以获得response 反馈信息,再用response.read()即可获得页面的源码. 最简单 ...
- lib 和 dll 的区别、生成以及使用详解
首先介绍一下静态库(静态链接库).动态库(动态链接库)的概念,首先两者都是代码共享的方式. 静态库:在链接步骤中,连接器将从库文件取得所需的代码,复制到生成的可执行文件中,这种库称为静态库,其特点是可 ...
- 介绍50个 WordPress 动作挂钩
WordPress 之所以能成为世界上最受欢迎的网页内容管理系统,原因就在于它的高度灵活性和可塑性,而这种灵活性和可塑性正是由“挂钩”(Hooks)简洁宜用的结构所决定的.可以说,没有过滤挂钩(Fil ...
- mysql数据库表间内外链接详解
1. 内连接(自然连接) 2. 外连接 (1)左外连接 (左边的表不加限制)(2)右外连接(右边的表不加限制)(3)全外连接(左右两表都不加限制) 3. 自连接(同一张表内的连接) SQL的标准语法: ...
- LinkedList类
LinkedList类 LinkedList类和ArrayList,Vector基本相同,都有增.删.改.查等方法.LinkedList是继承List接口. import java.util.*; p ...
- MPAndroidChart
该库的可扩展性强,代码相对规范,最近一次更新有很大改进,如果不喜欢AChartEngine的过于复杂可以考虑在此库的基础上开发自己的图表类. linechart 填充式lineChart 单条线的Li ...
- linux虚拟主机wdcp系列教程之四
当我们安装了网站服务管理系统wdcp之后,在使用过程中可能会出现这样或那样的疑问,下面给大家整理几点出来,方便大家学习. 1.wdcp支持的在线解压 有时小文件数据量,但整个目录上传会比较慢,如果打包 ...
- UOJ #148. 【NOIP2015】跳石头 二分
#148. [NOIP2015]跳石头 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://uoj.ac/problem/148 Descripti ...
- Codeforces Gym 100637A A. Nano alarm-clocks 前缀和
A. Nano alarm-clocks Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/p ...