Solved

Regex.Split: text with delimiters

Posted on 2011-02-11
4
635 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
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 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

895 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

14 Experts available now in Live!

Get 1:1 Help Now