namespace: System.Text.RegularExpressions

Regular Expressions in .NET
namespace: System.Text.RegularExpressions

Regular Expression is a very powerful and fast tool for parsing Strings for complex patterns, for validating user input, replacing specific values in a string etc…

One of the most common situations for using RegEx would be checking if the user entered a valid e-mail address (I’ll explain the pattern in detail after the code):

Dim myInput as String = txtInput.Text.Trim() 

Dim pattern as String = "^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$" 

Dim myRegEx As New System.Text.RegularExpressions.Regex(pattern) 

If myRegEx.IsMatch(myInput) Then 
    MessageBox.Show("E-Mail is valid") 
Else 
    MessageBox.Show("Please enter a valid E-Mail address!!") 
End If

The code is quite self explaining exept the pattern
[1]+(?:.[\w-]+)*@(?:[\w-]+.)+[a-zA-Z]{2,7}$”
…which means the following:

^ → Matches the beginning of the string or a line if RegexOptions.Multiline is set. It can also match itself but only if it occurs in the pattern - not at the beginning

[\w-]+ → Matches one or more occurence (“+”) or a alphanumeric char (“\w”) or a “-”

(?:.[\w-]+)* → This is a group that may occur any number (“*” - zero to any times) which matches a literal “.” plus [\w-]+ (see above)

@ → matches a “@”

(?:[\w-]+.)+ → Again a group which has to occur at least once (“+”) and matches a [\w-]+ followed by a “.”

[a-zA-Z]{2,7} → Matches 2 to 7 occurences (“{2-7}”) of upper (“A-Z”) or lower (“a-z”) letter

$ → Matches the end of a string (or a line - when RegexOptions.Multiline is set)

The next thing we’ll have a look at is Replacing

As I couldn’t figure out a better example we’re going to replace every word that starts with an “a” with the same word but make its first letter uppercase.

Dim myInput as String = "This is a string in which any word containing alphanumeric characters" & _ 
    " and starting with a lower case <a> will be replaced" 

Dim pattern as String = "(?<![\w])(?<First>a)(?<Other>\w*)" 

Dim myRegEx as new System.Text.RegularExpressions.Regex(pattern) 

Console.WriteLine(myInput) 

If myRegEx.IsMatch(myInput) Then 
    Console.WriteLine(myRegEx.Matches(myInput).Count.ToString() + " Matches found!") 

    Dim result as String = myRegEx.Replace(myInput, "A${Other}") 

    Console.WriteLine(result) 
End If

again the pattern in detail:

(?<![\w]) -> This is a negative lookbehind group - telling the parser to match only if the match is not preceded by an alphanumeric character - this character (for example a whitespace) will not be included in the match. we need this to match only at the beginning of a word

(?<First>a) -> This is a named group “(?<groupname>group-pattern)” which simply matches the starting “a” of our word…

(?<Other>\w*) -> Again a named group which matches the characters following the “a” as long as they are alphanumeric (for example all characters until the next “.” or Space)

Now the next thing we do is replace the Matches.

The replacement string may look a bit strange, but it’s simple to explain:

The “A” just stands for a literal uppercase “a”

The “${Other}” just tells the Regex to insert the contents of the named group “Other” at this place

…the Regular Expression concept is a very big topic you could write books on it - but as I don’t have the time to do so my FAQ ends here.
If you’ve got more questions then just ask me!

or take a look at the following links (most of them only provide VBScript or C# Code - but it’s easy to transfer to vb.NET):
what are Regular Expressions
Regular Expressions in C#
again C#
A RegExPattern Library
Regular Expressions in vb.net
Validation with Regular Expressions

A tool I recommend you to use for creating your own RegexPatterns is Expresso

I hope you learned something from this and enjoyed reading!


  1. \w- ↩︎