In this article I will discuss about regular expression (or RegEx in short) class in .Net.
Regular Expression is a big topic and can't be covered in a single article. I am trying to explain it in brief.
In .NET there is a strong support for regular expression class. A regular expression is a string that is used to describe match based on certain rule. Regular expression is a very powerful tool for pattern matching.
Regular Expression can be found in System.Text.RegularExpressions namespace.
Example for using regular expression:
If Regex.IsMatch("ASP.Net", "ASP") Then
MessageBox.Show("match")
Else
MessageBox.Show("no match")
End If
Above example ("where "ASP" is a pattern") search for the matching world "ASP" in the string "ASP.NET", if match found, shows message "match".
Similar way different pattern can be passed for searching a particular string.
Note: Regular Expression is a case sensitive; "a" is different from "A".
Example:
| String | Pattern | Result |
| Sales001 | Sale. | True |
| Sal123 | Sale. | False |
| Sale.xls | Sale. | True |
| Sale.xls | sale. | False |
| Sale | .a. | True |
Some of the list of patterns.
| Pattern | Matches |
| *. | Searches any string |
| Sales$ | Searches string which ends with "Sales" Example : Sales, finale Sales, year 2007 Sales |
| ^v[i,v]k$ | vik, vvk (string starts with v and ends with k and contains i and v) |
NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# Corner (http://www.c-sharpcorner.com/).