When you think ASP, think...
Recent Articles
All Articles
ASP.NET Articles
Related Web Technologies
User Tips!
Coding Tips

Sample Chapters
JavaScript Tutorials
MSDN Communities Hub
Official Docs
Stump the SQL Guru!
XML Info
Author an Article

The 4 Guys Present: ASPFAQs.com

Jump to a FAQ
Enter FAQ #:
..or see our 10 Most Viewed FAQs.

4GuysFromRolla.com : ASP FAQS : Regular Expressions


How do I use regular expressions from an ASP.NET Web page?

[Print this FAQ]

Answer: 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 Import directive.)

The .NET 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 Replace method: RegexOptions.IgnoreCase.

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 (re.).

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.

Happy Programming!

-- Read the official documentation for the Regex class...

FAQ posted by Scott Mitchell at 3/12/2002 12:28:27 PM to the Regular Expressions category. This FAQ has been viewed 70,022 times.

Do you have a FAQ you'd like to suggest? Suggestions? Comments? If so, send it in! Also, if you'd like to be a FAQ Admin (creating/editing FAQs), let me know! If you are looking for other FAQs, be sure to check out the 4Guys FAQ and Commonly Asked Messageboard Questions!

Most Viewed FAQs:

1.) How can I format numbers and date/times using ASP.NET? For example, I want to format a number as a currency. (761643 views)
2.) I am using Access and getting a 80004005 error (or a [Microsoft][ODBC Microsoft Access Driver] The Microsoft Jet database engine cannot open the file '(unknown)' error) when trying to open a connection! How can I fix this problem? (207777 views)
3.) How can I convert a Recordset into an array? Also, how can I convert an array into a Recordset? (202549 views)
4.) How can I quickly sort a VBScript array? (196039 views)
5.) How can I find out if a record already exists in a database? If it doesn't, I want to add it. (156019 views)
6.) How do I display data on a web page using arrays instead of Do...While...MoveNext...???... (152331 views)
7.) When I get a list of all files in a directory via the FileSystemObject, they aren't ordered in any reasonable way. How can I sort the files by name? Or by size? Or by date created? Or... (140381 views)
8.) For session variables to work, must the Web visitor have cookies enabled? (110162 views)
9.) Can I send emails without using CDONTS? (107083 views)
10.) How can I take the result of a SELECT...MULTIPLE or a group of same-named checkboxes and turn it into a query? That is, if the user selects 3 answers, how can I construct a query that looks for all 3? (106308 views)
Last computed at 9/17/2007 3:22:00 AM

ASP.NET [1.x] [2.0] | ASPFAQs.com | Advertise | Feedback | Author an Article