May 13, 2021 vb.net
Regular expressions are patterns that can match input text. . . T he Net framework provides a regular expression engine that allows this match. P atterns consist of one or more character text, operators, or constructs.
There are various categories of characters, operators, and constructs that allow you to define regular expressions. C lick the links below to find these structures.
Regular expression classes are used to represent a regular expression.
Regular expression classes have the following common methods:
SN | Methods and instructions |
---|---|
1 |
Public Function IsMatch (input As String) As Boolean
Represents whether the regular expression specified in the regular expression constructor found a match in the specified input string. |
2 |
Public Function IsMatch (input As String, startat As Integer ) As Boolean The common function IsMatch (entered as a string, startat as an integer) as Boolean Indicates whether the regular expression specified in the Regex constructor finds a match in the specified input string, starting at the starting position specified in the string. |
3 |
Public Shared Function IsMatch (input As String, pattern As String ) As Boolean The common sharing function IsMatch (input as a string, pattern as a string) as Boolean Indicates whether the specified regular expression found a match in the specified input string. |
4 |
Public Function Matches (input As String) As MatchCollection The common function matches (enters as a string) as MatchCollection Search the specified input string for all appearances of the regular expression. |
5 |
Public Function Replace (input As String, replacement As String) As String The common function replaces (input as a string, replace as a string) as a string In the specified input string, replace all strings that match the regular expression pattern with the specified replacement string. |
6 |
Public Function Split (input As String) As String() Public function (input as string) as string () Insert the input string into an array of substrings that specify a position defined by a regular expression pattern in the regular expression constructor. |
For a complete list of methods and properties, see the Microsoft documentation.
The following example matches a word that begins with an "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, "SS*") Console.ReadKey() End Sub End Module
When the above code is compiled and executed, it produces the following results:
Matching words that start with 'S': The Expression: SS* Splendid Suns
The following example matches a word that begins with "m" and ends with "e":
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 = "make a maze and manage to measure it" Console.WriteLine("Matching words that start with 'm' and ends with 'e': ") showMatch(str, "mS*e") Console.ReadKey() End Sub End Module
When the above code is compiled and executed, it produces the following results:
Matching words start with 'm' and ends with 'e': The Expression: mS*e make maze manage measure
This example replaces the extra white space:
Imports System.Text.RegularExpressions Module regexProg Sub Main() Dim input As String = "Hello World " Dim pattern As String = "s+" Dim replacement As String = " " Dim rgx As Regex = New Regex(pattern) Dim result As String = rgx.Replace(input, replacement) Console.WriteLine("Original String: {0}", input) Console.WriteLine("Replacement String: {0}", result) Console.ReadKey() End Sub End Module
When the above code is compiled and executed, it produces the following results:
Original String: Hello World Replacement String: Hello World