[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Regex.Split: text with delimiters

Posted on 2011-02-11
4
Medium Priority
?
646 Views
Last Modified: 2012-06-22
I need to split a string like "aaa-bbb/ccc:ddd", based ont the following delimiters: -,/,:. I would like to get an array like:

aaa-
bbb/
ccc:
ddd
I tried using Regex.Split, but I can only manage to get  an array with no deliminters  (aaa, bbb, ccc, ddd) or another one like: aaa, -, bbb, /

I would like to avoid working from this last version and then appending the delimiters to the text.

Thanks
Imports System.Text.RegularExpressions


Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Str As String = "aaa-bbb/ccc:ddd"
        Dim pattern As String = "(-)|(/)|(:)"

        For Each result As String In Regex.Split(Str, pattern).Where(Function(s) s <> "-" And s <> ":" And s <> "/")
            Debug.Print(result)
        Next

Open in new window

0
Comment
Question by:Pakhu1
4 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 34874780
I sux at RegExs...how about this approach?
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Str As String = "aaa-bbb/ccc:ddd"
        Dim delimiters As String = "-/:"
        Dim values As List(Of String) = MySplit(Str, delimiters)
        For i As Integer = 0 To values.Count - 1
            Debug.Print(i & ": " & values(i))
        Next
    End Sub

    Private Function MySplit(ByVal data As String, ByVal delimiters As String) As List(Of String)
        Dim removed As Boolean = True
        Dim values As New List(Of String)
        Dim sb As New System.Text.StringBuilder(data)
        While sb.Length > 0 AndAlso removed
            removed = False
            For i As Integer = 0 To sb.Length - 1
                If delimiters.Contains(sb(i)) Then
                    values.Add(sb.ToString(0, i + 1))
                    sb.Remove(0, i + 1)
                    removed = True
                    Exit For
                End If
            Next
            If Not removed AndAlso sb.Length > 0 Then
                values.Add(sb.ToString)
            End If
        End While
        Return values
    End Function

End Class

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 34876814
Regex.Split is not always the best approach.

Here is one possibility, using Regex.Matches:

Dim input As String = "aaa-bbb/ccc:ddd"

Dim pattern As String = "([a-z]*(\-|/|:)?)"

Dim matchList As MatchCollection = Regex.Matches(input, pattern)

Dim result As New List(Of String)()

For Each match As Match In matchList
    If Not String.IsNullOrEmpty(match.Value) Then
         result.Add(match.Value)
    End If
Next

Open in new window

0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 2000 total points
ID: 34880087
Just change your pattern as follows:

Dim pattern As String = "(?<=[-/:])"

I hope this helps.
0
 

Author Closing Comment

by:Pakhu1
ID: 34881237
Thnks
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question