[转]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 ...
随机推荐
- svn备份与恢复
公司把SVN服务器从windows迁移到linux,其主要步骤记录如下. windows上备份 "C:\Program Files\VisualSVN Server\bin\svnadmin ...
- [iOS UI进阶 - 3.0] 触摸事件的基本处理
A.需要掌握和练习的 1.介绍事件类型2.通过按钮的事件处理引出view的事件处理3.响应者对象 --> UIResponder --> UIView4.view的拖拽* 实现触摸方法,打 ...
- UVa 1640 The Counting Problem (数学,区间计数)
题意:给定两个数m, n,求从 m 到 n 中0-9数字各出现了多少次. 析:看起来挺简单的,其实并不好做,因为有容易想乱了.主要思路应该是这样的,分区间计数,先从个位进行计,一步一步的计算过来.都从 ...
- ActiveMQ学习笔记(一) JMS概要
(一)什么是JMS jms即Java消息服务(Java Message Service)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送 ...
- kettle内存溢出
ETL工具kettle,在老版设计后,使用新版时,居然发生了内存溢出的错误: 出现: java heap 或者 OutOfMemory等字样 这是kettle分配的内存不足. 在kettle的运行路径 ...
- Linux设置禁止用户登陆
Linux设置禁止用户登陆 vim /etc/shadow 第二栏(密码栏)设为*,会丢失密码 usermod -L username # -L Lock; -U Unlock chsh userna ...
- 去除Linq to Sql 查询结果中的空格
原来的写法: Dim db = From city In DataContext1.AddressCity Where city.ProvinceID = dgvAddress(2, e.RowInd ...
- 在 SUSE 11 sp2 上安装 freeradius
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- Android访问WebService的两种方法
首先解释一下WebService:WebService是一种基于SOAP协议的远程调用标准.通过WebService可以将不同操作系统平台,不同语言.不同技术整合到一起.详细见:http://baik ...
- node搜索codeforces 3A - Shortest path of the king
发一下牢骚和主题无关: 搜索,最短路都可以 每日一道理 人生是洁白的画纸,我们每个人就是手握各色笔的画师:人生也是一条看不到尽头的长路,我们每个人则是人生道路的远足者:人生还像是一块神奇的土地 ...