本文转自: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. 改变seekbar的游标图片大小

    url: http://stackoverflow.com/questions/9699951/changing-size-of-seekbar-thumb The most flexible way ...

  2. 【转】Maven实战(四)---多模块项目---JBOSS部署问题

    原文出自于:http://blog.csdn.net/liutengteng130/article/details/41622681      感谢! 这几天在搭框架中仅仅是JBOSS就遇到了很多问题 ...

  3. homework-04 单词方阵

    问题描述 本次作业的题目要求利用给定的一组单词生成一个矩阵,矩阵的每个位置由一个字母填充,单词表中的每一个单词可以匹配矩阵中一段连续的序列,这段序列可以是横向,纵向或者是45度斜角方向,单词可以由左向 ...

  4. Spring Named Parameters examples in SimpleJdbcTemplate

    In JdbcTemplate, SQL parameters are represented by a special placeholder "?" symbol and bi ...

  5. TypeScript学习笔记(三):类

    类 在TypeScript中,类似于C#的结构,即一个文件中可以存在多个类,且文件名可以任意取,我们先看一个简单的类的示例. class Person { private name: string; ...

  6. HTML5画布Canvas

    一.Canvas概念介绍 1.概念 Canvas : 画布 2.作用 : HTML5 Canvas 元素用于图形的绘制, 通过脚本(通常是JavaScript)来完成.它本身只是个图形容器,必须使用脚 ...

  7. PostgreSQL的initdb 源代码分析之五

    接前面,继续分析: putenv("TZ=GMT") 设置了时区信息. find_other_exec(argv[0], "postgres", PG_BACK ...

  8. 关于MVC中DropDownListFor的一个bug

    如以下代码: //后台 代码 ViewData["source_type"] = new List<SelectListItem> { "}, "} ...

  9. Codeforces Gym 100286B Blind Walk DFS

    Problem B. Blind WalkTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/cont ...

  10. 自定义的带tab的可左右滑动的viewpager之二viewpager与fragment不兼容

    总的来说,这个TAB用起来还算方便吧 不过随着用的地方多起来,发现了一些问题,比如下面这个界面: TAB1和TAB2都是表单,保存按钮对两个TAB都有效:若当前显示TAB1,点击保存则保存TAB1的f ...