[转]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 10 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 
$1, $2        - 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,"	","    ")

C#
'Convert tab characters into 4 spaces
sInput  = Regex.Replace(sInput,"	","    ");


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}","$$$&");
 
原文地址:https://www.cnblogs.com/freeliver54/p/3240417.html