TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Regex.Match Examples: Regular Expressions

Use Regex from System.Text.RegularExpressions. Match Strings based on patterns.
Regex. For testing and manipulating text, the Regex class is useful. With Regex, we use a text-processing language. This language easily handles string data.
Functions. With Match, we search strings. And with Replace, we change those we find. And often RegexOptions are used to change how these functions are evaluated.
Match example. This program uses Regex. Please notice the System.Text.RegularExpressions namespace—this is important to get started with regular expressions.

Step 1: We create a Regex object. The Regex pattern "\d+" matches one or more digit characters together.

Step 2: We invoke the Match Function on the Regex instance. This returns a Match (which we test next).

Step 3: We test if the match is successful. If it is, we print (with Console.WriteLine) its value—the string "77."

VB.NET program that uses Regex Imports System.Text.RegularExpressions Module Module1 Sub Main() ' Step 1: create Regex. Dim regex As Regex = New Regex("\d+") ' Step 2: call Match on Regex. Dim match As Match = regex.Match("Dot 77 Perls") ' Step 3: test the Success bool. ' ... If we have Success, write the Value. If match.Success Then Console.WriteLine(match.Value) End If End Sub End Module Output 77 Pattern details: \d Match digit char. + Match 1 or more chars.
IgnoreCase. Next, we use different syntax, and an option, for Match. We call the Regex.Match shared Function—no Regex object is needed. We then specify an option, RegexOptions.IgnoreCase.

IgnoreCase: This enum value, a constant, specifies that lower and uppercase letters are equal.

VB.NET program that uses RegexOptions.IgnoreCase Imports System.Text.RegularExpressions Module Module1 Sub Main() ' Match ignoring case of letters. Dim match As Match = Regex.Match("I like that cat", "C.T", RegexOptions.IgnoreCase) If match.Success Then ' Write value. Console.WriteLine(match.Value) End If End Sub End Module Output cat
Groups. This example uses Match and Groups. We specify the case of letters is unimportant with RegexOptions.IgnoreCase. And finally we test for Success on the Match object received.

Info: When we execute this program, we see the target text was successfully extracted from the input.

Groups index: We use the value 1 to get the first group from the Match. With Regex, indexing starts at 1 not 0 (don't ask why).

VB.NET program that uses Regex.Match Imports System.Text.RegularExpressions Module Module1 Sub Main() ' The input string. Dim value As String = "/content/alternate-1.aspx" ' Invoke the Match method. Dim m As Match = Regex.Match(value, _ "content/([A-Za-z0-9\-]+)\.aspx$", _ RegexOptions.IgnoreCase) ' If successful, write the group. If (m.Success) Then Dim key As String = m.Groups(1).Value Console.WriteLine(key) End If End Sub End Module Output alternate-1
Shared. A Regex object requires time to be created. We can instead share Regex objects, with the shared keyword. A shared Regex object is faster than shared Regex Functions.Shared

Therefore: Storing a Regex as a field in a module or class often results in a speed boost, when Match is called more than once.

Function: The Match function is an instance function on a Regex object. This program has the same result as the previous program.

VB.NET program that uses Match on Regex field Imports System.Text.RegularExpressions Module Module1 ''' <summary> ''' Member field regular expression. ''' </summary> Private _reg As Regex = New Regex("content/([A-Za-z0-9\-]+)\.aspx$", _ RegexOptions.IgnoreCase) Sub Main() ' The input string. Dim value As String = "/content/alternate-1.aspx" ' Invoke the Match method. ' ... Use the regex field. Dim m As Match = _reg.Match(value) ' If successful, write the group. If (m.Success) Then Dim key As String = m.Groups(1).Value Console.WriteLine(key) End If End Sub End Module Output alternate-1
Match, NextMatch. The Match() Function returns the first match only. But we can call NextMatch() on that returned Match object. This is a match that is found in the text, further on.

Tip: NextMatch can be called in a loop. This results in behavior similar to the Matches method (which may be easier to use).

For Each, For
VB.NET program that uses Match, NextMatch Imports System.Text.RegularExpressions Module Module1 Sub Main() ' Get first match. Dim match As Match = Regex.Match("4 and 5", "\d") If match.Success Then Console.WriteLine(match.Value) End If ' Get next match. match = match.NextMatch() If match.Success Then Console.WriteLine(match.Value) End If End Sub End Module Output 4 5
IsMatch. This returns true if a String matches the regular expression. We get a Boolean that tells us whether a pattern matches. If no other results are needed, IsMatch is useful.

Here: This program introduces the IsValid Boolean function, which computes the result of the Regex.IsMatch function on its parameter.

Note: The regular expression pattern indicates any string of lowercase ASCII letters, uppercase ASCII letters, or digits.

VB.NET program that uses Regex.IsMatch function Imports System.Text.RegularExpressions Module Module1 Function IsValid(ByRef value As String) As Boolean Return Regex.IsMatch(value, "^[a-zA-Z0-9]*$") End Function Sub Main() Console.WriteLine(IsValid("dotnetCodex0123")) Console.WriteLine(IsValid("DotNetPerls")) Console.WriteLine(IsValid(":-)")) End Sub End Module Output True True False
Start and ends. Matching the start and end of a String is commonly-needed. We use the metacharacters "^" and "$" to match the starts and ends of a string.

Note: IsMatch() evaluates these metacharacters in the same way that Match (or Matches) can—the result is different for each function.

VB.NET program that uses IsMatch, tests start and end Imports System.Text.RegularExpressions Module Module1 Sub Main() Dim value As String = "xxyy" ' Match the start with a "^" char. If Regex.IsMatch(value, "^xx") Then Console.WriteLine("ISMATCH START") End If ' Match the end with a "$" char. If Regex.IsMatch(value, "yy$") Then Console.WriteLine("ISMATCH END") End If End Sub End Module Output ISMATCH START ISMATCH END Pattern details: ^ Start of string. xx 2 x chars. yy 2 y chars. $ End of string.
Benchmark, Compiled Regex. To optimize Regex performance in VB.NET, we can use the RegexOptions.Compiled enum and store the Regex in a field. Here we test Compiled Regexes.

Version 1: This code uses a Regex field in the Module, and calls IsMatch on the field instance.

Version 2: Uses Regex.IsMatch directly with no stored Regex instance. This code does the same thing as version 1.

Result: The optimizations applied (a field, and RegexOptions.Compiled) makes regular expression testing much faster.

VB.NET program that times RegexOptions.Compiled Imports System.Text.RegularExpressions Module Module1 Dim _regex As Regex = New Regex("X.+0", RegexOptions.Compiled) Sub Version1() ' Use compiled regular expression stored as field. If _regex.IsMatch("X12340") = False Then Throw New Exception End If End Sub Sub Version2() ' Do not use compiled Regex. If Regex.IsMatch("X12340", "X.+0") = False Then Throw New Exception End If End Sub Sub Main() Dim m As Integer = 100000 Dim s1 As Stopwatch = Stopwatch.StartNew ' Version 1: use RegexOptions.Compiled. For i As Integer = 0 To m - 1 Version1() Next s1.Stop() Dim s2 As Stopwatch = Stopwatch.StartNew ' Version 2: do not compile the Regex. For i As Integer = 0 To m - 1 Version2() Next s2.Stop() Dim u As Integer = 1000000 Console.WriteLine(((s1.Elapsed.TotalMilliseconds * u) / m).ToString("0.00 ns")) Console.WriteLine(((s2.Elapsed.TotalMilliseconds * u) / m).ToString("0.00 ns")) End Sub End Module Output 131.78 ns IsMatch, RegexOptions.Compiled 484.66 ns IsMatch
Matches. This function is used to locate and return parts of the source String in separate variables. To capture groups, we use parentheses in a Regex pattern.Regex.MatchesRegex.Matches Quote
Some examples. In these programs, I remove HTML tags. Be careful—this does not work on all HTML. I also count words in English text (this is also not perfect).HTML TagsWord Counts
Replace. This function takes an optional MatchEvaluator. It will perform both a matching operation and a replacement of the matching parts.Regex.Replace
Split. Sometimes the String Split function is just not enough for our splitting needs. For these times, try the Regex.Split function.Regex.Split
Files. See how you can read in the lines of a file, and parse each line, accessing only a part of the text from each line. The StreamReader type and Regex.Match Function are used.Regex File
In some programs, a Regex function is the easiest way to process text. In others, it adds complexity for little gain. As developers, we must decide when to use Regex.
At its core, the Regex type exposes a text-processing language, one built upon finite deterministic automata. Tiny programs efficiently manipulate text.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf