ARTICLE

Split and Match Methods in VB.NET

Posted by Manish Tewatia Articles | Visual Basic Language August 15, 2010
In this article I will explain you about the Split and Match Methods in VB.NET.
Reader Level:

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(metaExp, RegexOptions.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.

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor