Regular Expression in VB.net with Examples (RegEx Class)

A Regular expression in VB.net is a set of rules that can be used to match a piece of text. The .Net framework has an engine for regular expressions that makes this kind of matching possible.

One or more character literals, operators, or constructs make up a pattern.

We have learned how to use the Print Dialog Box in VB.net in the previous lesson. In this lesson we shall learn How To Write a Program for Regular Expression in VB.net.

What is Regular Expression in VB.net?

In VB.NET, the Regular Expression engine is called “Regex.” It can be used to quickly parse a lot of text to find specific patterns of characters so that text substrings can be extracted, edited, replaced, or deleted.

Constructs for Defining Regular Expressions

You can define regular expressions by using different kinds of characters, operators, and “constructs.” Click on the links below to find these ideas.

1. Character Escapes

Basically, these are the special characters, also called escape characters. In a regular expression, the backslash character () means that the character that comes after it is either a special character or should be taken literally.

2. Character Classes

A character class is a group of characters that any one of them fits into.

3. Anchors

Anchors make it possible for a match to succeed or fail based on where it is in the string. The anchors are listed in the table below.

AssertionDescriptionPatternMatches
^The match must start at the beginning of the string or line.^\d{3}“567” in “567-777-“
$The match must occur at the end of the string or before \n at the end of the line or string.-\d{4}$“-2012” in “8-12-2012”
\AThe match must occur at the start of the string.\A\w{3}“Code” in “Code-007-“
\ZThe match must occur at the end of the string or before \n at the end of the string.-\d{3}\Z“-007” in “Bond-901-007”
\zThe match must occur at the end of the string.-\d{3}\z“-333” in “-901-333”
\GThe match must occur at the point, where the previous match ended.\\G\(\d\)“(1)”, “(3)”, “(5)” in “(1)(3)(5)[7](9)”
\bThe match must occur on a boundary between a \w (alphanumeric) and a \W (non-alphanumeric) character.\w“R”, “o”, “m” and “1” in “Room#1”
\BThe match must not occur on a \b boundary.\Bend\w*\b“ends”, “ender” in “end sends endure lender”
List of Anchors

4. Grouping Constructs

Grouping constructs separate parts of a regular expression into subexpressions and capture parts of an input string. In the next table, you can see a list of the grouping constructs.

Grouping constructDescriptionPatternMatches
( subexpression )Captures the matched subexpression and assigns it a zero-based ordinal number.(\w)\1“ee” in “deep”
(?< name >subexpression)Captures the matched subexpression into a named group.(?< double>\w)\k< double>“ee” in “deep”
(?< name1 -name2 >subexpression)Defines a balancing group definition.(((?’Open’\()[^\(\)]*)+((?’Close-Open’\))[^\(\)]*)+)*(?(Open)(?!))$“((1-3)*(3-1))” in “3+2^((1-3)*(3-1))”
(?: subexpression)Defines a noncapturing group.Write(?:Line)?“WriteLine” in “Console.WriteLine()”
(?imnsx-imnsx:subexpression)Applies or disables the specified options within subexpression.A\d{2}(?i:\w+)\b“A12xl”, “A12XL” in “A12xl A12XL a12xl”
(?= subexpression)Zero-width positive lookahead assertion.\w+(?=\.)“is”, “ran”, and “out” in “He is. The dog ran. The sun is out.”
(?! subexpression)Zero-width negative lookahead assertion.\b(?!un)\w+\b“sure”, “used” in “unsure sure unity used”
(?< =subexpression)Zero-width positive lookbehind assertion.(?< =19)\d{2}\b“51”, “03” in “1851 1999 1950 1905 2003”
(?< ! subexpression)Zero-width negative lookbehind assertion.(?< !19)\d{2}\b“ends”, “ender” in “end sends endure lender”
(?> subexpression)Nonbacktracking (or “greedy”) subexpression.[13579](?>A+B+)“1ABB”, “3ABB”, and “5AB” in “1ABB 3ABBC 5AB 5AC”
List of Grouping Constructs

5. Quantifiers

Quantifiers tell how many times the previous element (which could be a character, a group of characters, or a class of characters) must be in the input string for a match to happen.

QuantifierDescriptionPatternMatches
*Matches the previous element zero or more times.\d*\.\d“.0”, “19.9”, “219.9”
+Matches the previous element one or more times.“be+”“bee” in “been”, “be” in “bent”
?Matches the previous element zero or one time.“rai?n”“ran”, “rain”
{ n }Matches the previous element exactly n times.“,\d{3}”“,043” in “1,043.6”, “,876”, “,543”, and “,210” in “9,876,543,210”
{ n ,}Matches the previous element at least n times.“\d{2,}”“166”, “29”, “1930”
{ n , m }Matches the previous element at least n times, but no more than m times.“\d{3,5}”“166”, “17668” “19302” in “193024”
*?Matches the previous element zero or more times, but as few times as possible.\d*?\.\d“.0”, “19.9”, “219.9”
+?Matches the previous element one or more times, but as few times as possible.“be+?”“be” in “been”, “be” in “bent”
??Matches the previous element zero or one time, but as few times as possible.“rai??n”“ran”, “rain”
{ n }?Matches the preceding element exactly n times.“,\d{3}?”“,043” in “1,043.6”, “,876”, “,543”, and “,210” in “9,876,543,210”
{ n ,}?Matches the previous element at least n times, but as few times as possible.“\d{2,}?”“166”, “29”, “1930”
{ n , m }?Matches the previous element between n and m times, but as few times as possible.“\d{3,5}?”“166”, “17668” “193”, “024” in “193024”
List of Quantifiers

6. Backreference Constructs

Backreference constructs make it possible for the same regular expression to find a subexpression that has already been matched.

In the table below, you can see a list of these terms:

Backreference constructDescriptionPatternMatches
\ numberBackreference. Matches the value of a numbered subexpression.(\w)\1“ee” in “seek”
\k< name >Named backreference. Matches the value of a named expression.(?< char>\w)\k< char>“ee” in “seek”
List of Backreference Constructs

7. Alternation Constructs

Alternation constructs are ways to change a regular expression so that it can match either/or. The different types of alternation are shown in the table below:

Alternation constructDescriptionPatternMatches
|Matches any one element separated by the vertical bar (|) character.th(e|is|at)“the”, “this” in “this is the day. “
(?( expression )yes | no )Matches yes if expression matches; otherwise, matches the optional no part. Expression is interpreted as a zero-width assertion.(?(A)A\d{2}\b|\b\d{3}\b)“A10”, “910” in “A10 C103 910”
(?( name )yes | no )Matches yes if the named capture name has a match; otherwise, matches the optional no.(?< quoted>”)?(?(quoted).+?”|\S+\s)Dogs.jpg, “Yiska playing.jpg” in “Dogs.jpg “Yiska playing.jpg””
List of Alternation Constructs

8. Substitutions

In replacement patterns, Substitutions are used. The Substitutions are listed in the table below.

CharacterDescriptionPatternReplacement patternInput stringResult string
$numberSubstitutes the substring matched by group number.\b(\w+)(\s)(\w+)\b$3$2$1“one two”“two one”
${name}Substitutes the substring matched by the named groupname.\b(?< word1>\w+)(\s)(?< word2>\w+)\b${word2} ${word1}“one two”“two one”
$$Substitutes a literal “$”.\b(\d+)\s?USD$$$1“103 USD”“$103”
$&Substitutes a copy of the whole match.(\$*(\d*(\.+\d+)?){1})**$&“$1.30”“**$1.30**”
$`Substitutes all the text of the input string before the match.B+$`“AABBCC”“AAAACC”
$’Substitutes all the text of the input string after the match.B+$’“AABBCC”“AACCCC”
$+Substitutes the last group that was captured.B+(C+)$+“AABBCCDD”AACCDD
$_Substitutes the entire input string.B+$_“AABBCC”“AAAABBCCCC”
List of Substitutions

9. Miscellaneous constructs

Following are various miscellaneous constructs:

ConstructDefinitionExample
(?imnsx-imnsx)Sets or disables options such as case insensitivity in the middle of a pattern.\bA(?i)b\w+\b matches “ABA”, “Able” in “ABA Able Act”
(?#comment)In-line comment. The comment ends at the first closing parenthesis.\bA(?#Matches words starting with A)\w+\b
[to end of line]X-mode comment. The comment starts at an unescaped # and continues to the end of the line.(?x)\bA\w+\b#Matches words starting with A
List of Miscellaneous Constructs

The VB.net Regex Class

A VB.net Regular Expression can be shown by the Regex Class.

Most of the time, the following methods are used with the Regex class:

#MethodsDescription
1.Public Function IsMatch (input As String) As BooleanIndicates whether the regular expression specified in the Regex constructor finds a match in a specified input string.
2.Public Function IsMatch (input As String, startat As Integer ) As BooleanIndicates whether the regular expression specified in the Regex constructor finds a match in the specified input string, beginning at the specified starting position in the string.
3.Public Shared Function IsMatch (input As String, pattern As String ) As BooleanIndicates whether the specified regular expression finds a match in the specified input string.
4.Public Function Matches (input As String) As MatchCollectionSearches the specified input string for all occurrences of a regular expression.
5.Public Function Replace (input As String, replacement As String) As StringIn a specified input string, replaces all strings that match a regular expression pattern with a specified replacement string.
6.Public Function Split (input As String) As String()Splits an input string into an array of substrings at the positions defined by a regular expression pattern specified in the Regex constructor.
Methods and Descriptions of Regex Class

Example Program of Regular Expressions VB.net

This example pairs words that start with the letter “S.”

Imports System.Text.RegularExpressions
Module regexProg
   Sub showMatch(ByVal text As String, ByVal expr As String)
      Console.WriteLine("The Expression: " + expr)
      Dim mc As MatchCollection = Regex.Matches(text, expr)
      Dim m As Match
      For Each m In mc
          Console.WriteLine(m)
      Next m
   End Sub
   Sub Main()
      Dim str As String = "A Thousand Splendid Suns"
      Console.WriteLine("Matching words that start with 'S': ")
      showMatch(str, "\bS\S*")
      Console.ReadKey()
   End Sub
End Module

Program Output:

Matching words that start with ‘S’:
The Expression: \bS\S*
Splendid
Suns

Summary

This tutorial we have discussed on how to create a Regular Expression in VB.net using Microsoft Visual Studio. A Regex is the easiest way to process text in some programs. At its core, the Regex type shows a language for processing text that is based on finite deterministic automata.


Leave a Comment