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}") |
| 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}","$$$&"); |