Regular Expressions in VB.NET
by Brandon on Nov 9th in VB.NET
The usage of regular expressions can be a powerful thing. With it you can prevent invalid input into your applications and ultimately prevent system crashes or prevent security threats. In VB.NET, it is a fairly simple process which I will go over right now.
The first step is to add the regular expression namespace to your project if it is not already present. At the top of the code outside of any class you will want to have the following:
Imports System.Text.RegularExpressions
This namespace tells the compiler that you want to utilize the attributes and methods within System.Text.RegularExpressions. Without it you will not be able to use regular expressions within your code.
The next step is to declare the regular expression and give it an expression of which it will compare with.
Dim regexObj As New Regex("^\d")
As shown, you declare a variable as a new regex type and then simply pass it an expression through the parameters. This then creates a new regular expression and stores it in the variable, in this case named regexObj.
That pretty much all there is to it. The regular expression comes with a list of methods in which you can use the expression. For example:
regexObj.isMatch(varName) regexObj.Match(varName) regexObj.Matches(varName)