本文转自: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的更多相关文章

  1. Regex.Replace的基本用法

    Regex构造函数Regex(string pattern)Regex(string pattern,RegexOptions options)参数说明pattern:要匹配的正则表达式模式optio ...

  2. 正则表达式之Regex.Replace()用法

    正则表达式替换匹配到的字符串 string txt = "AAA12345678AAAA"; //匹配到的连续数字的前4位用*替换 string m =Regex.Replace( ...

  3. vb.net 使用 Regex Replace 正则 替换 Html字串的table中tbody第一个tr下的td为th

    本次示例效果如下: TextBox1中输入如下字符串: 12<table><tbody><tr><td>1<br/>11</td> ...

  4. C#正则表达式replace用法

    Regex构造函数Regex(string pattern)Regex(string pattern,RegexOptions options)参数说明pattern:要匹配的正则表达式模式optio ...

  5. Inside TSQL Querying - Chapter 3. Query Tuning

    Tuning Methodology When dealing with performance problems, database professionals tend to focus on t ...

  6. JavaScript常用表单验证正则表达式(身份证、电话号码、邮编、日期、IP等)

    身份证正则表达式 //身份证正则表达式(15位)isIDCard1=/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;//身份证正则表达式 ...

  7. Inside Microsoft SQL Server 2008: T-SQL Querying 读书笔记之查询优化

    一. 自顶向下优化方法论 1. 分析实例级别的等待 在实例级找出什么类型的等待占用大部分的时间,通过sys.dm_os_wait_stats select wait_type, --等待类型 wait ...

  8. SQL点滴15—在SQL Server 2008中调用C#程序

    原文:SQL点滴15-在SQL Server 2008中调用C#程序 T-SQL的在执行普通的查询的时候是很高效的,但是在执行循环,判断这样的语句的时候效率就不那么的高了.这时可以借助CLR了,我们可 ...

  9. 在SQL Server 2008中调用.net,dll

    原文:在SQL Server 2008中调用.net,dll T-SQL的在执行普通的查询的时候是很高效的,但是在执行循环,判断这样的语句的时候效率就不那么的高了.这时可以借助CLR了,我们可以在SQ ...

随机推荐

  1. mysql的point类型查询处理

    mysql的point类型,很蛋疼的表示更习惯于key—value的lng,lat 假设不得不处理数据库字段poi是point类型,其中的数据为 : POINT(28.2789745229671 11 ...

  2. JDBC学习笔记(7)——事务的隔离级别&批量处理

    数据库事务的隔离级别 对于同时运行的多个事务, 当这些事务访问数据库中相同的数据时, 如果没有采取必要的隔离机制, 就会导致各种并发问题:脏读: 对于两个事务 T1, T2, T1 读取了已经被 T2 ...

  3. (转)从工程中删除Cocoapods

    1. 删除工程文件夹下的Podfile.Podfile.lock及Pods文件夹 2. 删除xcworkspace文件 3. 使用xcodeproj文件打开工程,删除Frameworks组下的Pods ...

  4. Web开源框架大汇总

    Struts 项目简介信息 Struts是一个基于Sun J2EE平台的MVC框架,主要是采用Servlet和JSP技术来实现的.由于Struts能充分满足应用开发的需求,简单易用,敏捷迅速,在过去的 ...

  5. 更新证书错误Code Sign error: Provisioning profile ‘XXXX'can't be found

    在Xcode中当你在更新了你得证书而再重新编译你的程序,真机调试一直会出现 Code Sign error: Provisioning profile ‘XXXX’ can't be found是不是 ...

  6. jQuery UI vs EasyUI

    几个UI框架的比较k: 目前工作中可能会常用到几个UI框架,如 Ext: http://docs.sencha.com/ext-js/4-1/#!/example 感觉其过于复杂,性能不高,所以一直没 ...

  7. PROCESS_YIELD()宏使用及过程分析<contiki学习笔记之八>

    好吧,昨晚上研究了switch()的底层实现原理--发现它并不是一般C语言教科书上那样所言,当然,这对于本身就非常熟悉汇编的同学来说,是小菜一碟.世界上,很多事情是巧合与必然的结合体,没有无缘无故的爱 ...

  8. struts2属性Struts2中属性接收参数中文问题和简单数据验证

    PS:今天上午,非常郁闷,有很多简单基础的问题搞得我有些迷茫,哎,代码几天不写就忘.目前又不当COO,还是得用心记代码哦! 一:如果表单提交数据中有中文时,尽量应用post方式. 需要在Struts. ...

  9. NFA与DFA

    正则表达式匹配,包含两个东西,一个是表达式,一个文本. NFA(Nondeterministic Finite Automaton),不确定有穷自动机,表达式主导,NFA去吃文本,贪婪算法吃下去,如果 ...

  10. C#操作MySQL数据库-----HelloWorld

    这里采用在visual studio 2010中通过MySql.Data.dll.MySql.Web.dll来连接mysql数据库, 之后便进行数据的插入和查询. Program.cs文件内容如下: ...