Solved

Regex.Split: text with delimiters

Posted on 2011-02-11
4
641 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

687 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