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…