Matching Regular Expressions (Regex) in ASP.NET

There may come a time when you want to match a Regular Expression against a string and you don’t necessarily want or need to use one of the built in ASP.Net Regular Expression Validators. For instance, using a validator, if the match doesn’t occur, you are not allowed to submit your form data. But, let’s say, you want to validate the data, but you don’t really mind if the data is a match or not and you merely want to send the user a message that the data didn’t match the correct format.

Using theĀ System.Text.RegularExpressions Namespace (don’t forget to import it!), you can accomplish this in code. Note the following function:

Function RegExMatch(sItem as String, sRegEx as String) as Boolean Dim r As Regex = New Regex(sRegEx) Dim m As Match = r.Match(sItem) Return m.Success End Function

Here, we’re taking the first argument in the function (sItem) and matching it against the Regular Expression, which is in the second argument. Since the Function returns a boolean, naturally, the only two possibilites are TRUE or FALSE.

So, to implement this in code, let’s create a scenario where you are entering parts into your online system. For the partnumber itself, you use the format:
####.## – where all characters must be numeric. The Regular Expression for this would be:
^[1-9][0-9][0-9][0-9]\.[0-9][0-9]

So, in your code, you can do something like this:
Continues…

Understanding Regular Expressions (Regex) in ASP.NET

This tutorial is not meant to be an exhaustive one, but merely a ‘look’ at some of the syntax used in Regular Expressions, so that it will not look quite so ‘foreign’ to you, the next time you look at a Regular Expression inside the Regular Expression Validator in ASP.Net.

The use of regular expressions is based on the contents of a string, matching criteria set in play by the assigned Regular Expression. It tests for a pattern within a string. For instance, let’s say the string to search comes from a text box called ‘Text1′. Let’s say that the Regular Expression (matching characters) we are searching for is any lower case letter. The Regular Expression would be [a-z]. Therefore, the string returned from ‘Text1.text’ will be searched and matched against the Regular Expression. Regular Expressions in ASP.Net, used in Validator Server Controls are kind of like applying rules to the text input. If the Regular Expression above were put into a validator for ‘Text1′, then, since anything other than lower case letters would NOT match, if we put in the number 6, it would FAIL validation. On the otherhand, ‘top’ would PASS validation.

At this point, we need to stop and discuss several characters (Metacharacters) we must come to understand when using Regular Expressions (Regex). Above, we saw two of them – the brackets ([])and the dash (-). First of all, we call any characters we use for search or matching, ‘Literals’. The ‘a’ and the ‘z’ in the example above are ‘Literals’. Next we come to the ‘Metacharacter’. Metacharacters in the above example are the brackets and the dash. You can also use the caret (^)/(Shift-6). Also, we can use the dollar sign ($), the question mark (?), the asterisk (*), the plus sign (+), and the period (.). The meanings for these Metacharacters are described below.

Continues…