Solved

Regex.Split: text with delimiters

Posted on 2011-02-11
4
634 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 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
Just change your pattern as follows:

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

I hope this helps.
0
 

Author Closing Comment

by:Pakhu1
Comment Utility
Thnks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now