||There are a plethora of regular expressions available on 4Guys. (See this page for a listing of regular expression articles and resources on 4Guys and on the Web.) However, the vast majority of the articles show using regular expressions from VBScript. Of course VBScript can only be used in a classic ASP Web page... so how does one use regular expressions in ASP.NET?|
Recall that with VBScript, regular expressions are handled through the
Regexp class. (Check out this demo to see some sample VBScript regular expression code.) With VB.NET (or C#, or any .NET-compatible programming language),you just need to use the
Regex class, which can be found in the
System.Text.RegularExpressions namespace. (Note that this namespace is automatically imported into your ASP.NET Web pages, so to use regular expressions through an ASP.NET Web page you do not need to include a separate
Regex class is syntactically very similar to the VBScript
Regexp class - it contains methods to find patterns, replace them, to test if a pattern exists, etc. One of the nice things about the
Regex class is that it has a large number of static methods. (Static methods are methods that can be used without needing to create an actual instance of the class.) Using these static methods, we can perform regular expression tasks in one line of code.
Some Examples, Please
The remainder of this FAQ will be spent examining some simple regular expression examples using the
Regex class. As always, you are highly encouraged to read the official documentation.
Our first example shows how to use the static version of the
Replace method to search for all instances of hell, replacing it with heck.
Dim uncensored as String
uncensored = "If you don't change your ways, you'll go to hell."
Dim censored as String
censored = Regex.Replace(uncensored, "\bhell\b", "heck")
The above searches for all instances of "hell" in the string
uncensored and replaces them with "heck," assigning the result to the string
censored. Note that the
\b specifies a word boundary (see this FAQ for more information on word boundaries). Also note that the above is case-sensitive, meaning if you have the word "Hell," it will not be replaced by "heck." To specify that the replace should be case-insensitive, you need to add another argument to the
Note that we could have done the above by using the non-static version of the
Replace method. To do this we need to create an instance of the
Regex object first. When creating it, we can specify that pattern via the constructor, like so:
Dim re as Regex = New Regex("\bhell\b")
censored = re.Replace(uncensored, "heck")
in this example we don't need to specify the pattern in the call to the
Replace method since we have already specified it in our constructor call. Also note that since this is a non-static call, we create an instance of the
Regex class and then, when calling
Replace, prepend the call with the name of the instance (
As you can (hopefully) see, the code needed for using regular expressions in ASP.NET is very close to the code used in classic ASP. Spending a few minutes with the docs should get you up to speed.
-- Read the official documentation for the