VB.NET Split and Match Methods

In this article I will explain you about the Split and Match Methods in VB.NET.
  • 1971

There are a few significant RegEx methods: 

The RegEx.Split method splits an input string into an array of substrings at the positions defined by a regular expression match. 

The RegEx.Replace method replaces all occurrences of a character pattern defined by a regular expression with a specified replacement character string. 

The RegEx.Matches method searches an input string for all occurrences of a regular expression and returns all the successful matches as if Match were called numerous times. 

There is also a MatchCollection class that represents the set of successful matches found by iteratively applying a regular expression pattern to the input string. 

The example below is shows the Split and Matches methods and the MatchCollection class. 

Example of Split and Match

    Imports System
    Imports System.Text.RegularExpressions
    Public Class RegExpSplit
        Public Shared Sub Main(ByVal args As String())
            Console.WriteLine("Enter a split delimeter ( default is [0-9 a-z A-Z]* ) : ")
            metaExp = 
Console.ReadLine()
            Console.WriteLine("Enter a meta string: ")
            Dim rets As String() = ParseExtnSplit(Console.ReadLine())
            If rets Is Nothing Then
                Console.WriteLine("Sorry no match")
            Else
                Console.WriteLine(rets.Length)
                For Each x As String In rets
                    Console.WriteLine(x)
                Next
            End
 If
            Console.WriteLine("Enter a match pattern ( default is [0-9 a-z A-Z]* ) : ")
            metaExp = 
Console.ReadLine()
            Console.WriteLine("Enter a meta string: ")
            rets = ParseExtnMatch(
Console.ReadLine())
            If rets Is Nothing Then
                Console.WriteLine("Sorry no match")
            Else
                Console.WriteLine(rets.Length)
                For Each x As String In rets
                    Console.WriteLine(x)
                Next
            End
 If
            Console.ReadLine()
        End Sub

        Public
 Shared Function ParseExtnSplit(ByVal ext As [String]As String()
            Dim rx As New Regex(metaExp)
            Return rx.Split(ext)
        End Function

        Public
 Shared Function ParseExtnMatch(ByVal ext As [String]As String()
            ' case insensitive match
            Dim rx As New Regex(metaExpRegexOptions.IgnoreCase)
            Dim rez As MatchCollection = rx.Matches(ext)
            Dim ret As String() = Nothing
            If
 rez.Count > 0 Then
                ret = New String(rez.Count) {}
                Dim i As Integer = 0
                While i < rez.Count
                    ret(i) = rez(i).ToString()
                    System.
Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
                End While
            End
 If
            Return
 ret
        End Function
        Private
 Shared metaExp As String = "[0-9 a-z A-Z]*"
    End Class

Output

SandM.gif
 

Conclusion

Hope this article would have helped you in understanding the Split and Match Methods in VB.NET.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.