Solved

Regex.Split: text with delimiters

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Reactjs with .NET 3 78
Why use this lambda? 12 65
vb.net convert long time to mm:ss 23 22
asp.net figure out error 3 26
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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