[转]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 ...
随机推荐
- HDU 5795 A Simple Nim (博弈 打表找规律)
A Simple Nim 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5795 Description Two players take turns ...
- 通用FASTREPORT打印模块及接口方法
untFastReport.dfm文件: object frmFastReport: TfrmFastReport OldCreateOrder = False Height = 405 Width ...
- jQuery基础学习6——基本选择器
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- UI进阶 KVO
KVO:(Key-Value-Observer)键值观察者,是观察者设计模式的一种具体实现 KVO触发机制:一个对象(观察者),监测另一对象(被观察者)的某属性是否发生变化,若被监测的属性发生的更改, ...
- Spring Controller参数为空串的处理方式
控制器参数为String类型 Spring框架接收到传入的空串后,此参数被赋值为空串,不为null. 控制器参数为非String类型 Spring框架接收到传入的空串后,此参数被赋值为null.
- C++常用容器
vector 顺序容器,和数组类似,可从尾部快速的插入和删除,可随机访问. vector的常用成员函数: #include<vector> std::vector<type> ...
- Linux下的图形界面——X Window的安装
X Window即X Windows图形用户接口,是一种计算机软件系统和网络协议,提供了一个基础的图形用户界面(GUI)和丰富的输入设备能力联网计算机.其中软件编写使用广义的命令集,它创建了一个硬件抽 ...
- C++转义字符使用
编码过程中字符串可能过长,这通常须要换行,对于换行转义字符\ ,使用时要保证\后无空格,否则会出现"error C2017:非法的转义字符 "错误 如 // ''\"后 ...
- char指针
1.在C语言中,没有字符串类型,因此使用char指针表示字符串. 2.那么问题来了,使用char* 表示字符串,到哪里是结尾呢?因此需要一个特殊的字符作为哨兵,类似迭代器中的end(),这个哨兵就是' ...
- vector 与map的下标操作
1.vector的下标操作不会添加元素,只能针对已经存在的元素操作. 2.map的下标操作具有副作用,key不存在,会在map中添加一个具有该key的新元素,新元素的value使用默认构造方法. 3. ...