Page view counter

These tutorials demonstrate selected features in ASP.NET version 2.0, but they are compatible with later versions of ASP.NET as well. For the current documentation, see the ASP.NET portal on the MSDN Web site.

 

 

   Welcome   |   ASP.NET   |   Web Services   |   Class Browser   
  |   I want my samples in...      

How Do I...? Common Tasks QuickStart Tutorial

How Do I...Use Regular Expressions to make replacements?



The Regular Expressions library can often ease the time it takes to generate string replacement functions. By specifying a pattern of strings to be replaced, you do not have to search for every possible variation of a string. Once a Regex object that matches every possible string to be replaced is created, the Replace method can be used to generate a result. The Replace method can be most easily used by passing in the source string and the replacement string. The Replace method will return the results as a String.

		
Dim digitregex As Regex = new Regex("(?<digit>[0-9])")
Dim Before As String = "Here is so4848me te88xt with emb4493edded numbers."
 ...
Dim After As String = DigitRegex.Replace(Before, "")
VB


You can also reuse the matched string in the replacement. In the previous snippet we have a named capture of ?<digit>. This named capture can be reused in the replacement string as ${digit}. Note that ordinal captures can be used as well, $123, which would evaluate to 123 captures in our pattern.

This example illustrates how to use the Replace method of Regex to remove all digits from the input string.

Example

VB RegexReplace.exe
View Source



Microsoft .NET Framework SDK QuickStart Tutorials Version 2.0
Copyright � 2005 Microsoft Corporation. All rights reserved.