Tom
Tom

Reputation: 11

How can I capture the following repeating data using a regular expression in .Net?

I'm trying to find the right RegEx to capture some (potentially) repeating data. I don't know how many times it will repeat. If I give an exanple of the data and what I want to capture can anyone point me in the right direction? It's the .Net regex engine (Visual Basic)

The data looks basically like this, and is delimited by $!$ when there is more than one occurance:

MyFunction('001$$String Description 1$!$002$$String Description 2');

I want to capture the following groups (of which there could be any number):

1: 001$$String Description 1

2: 002$$String Description 2

etc. etc.

I know this can be done with RegEx.Split and probably String.Split, but I still want to know if it's possible in a single capturing RegEx :) Any pointers?

Many thanks.

Upvotes: 0

Views: 242

Answers (1)

Tim Pietzcker
Tim Pietzcker

Reputation: 336258

You could do it using this regex:

Dim RegexObj As New Regex("MyFunction\('(?:(\d+\$\$[^$]+)(?:\$!\$)?)+'\);")

and then, after a successful match, something like this:

Dim MatchResults As Match = RegexObj.Match(SubjectString)
If MatchResults.Success Then
    Console.WriteLine("Matched text: {0}", MatchResults.Value)
    For Each capture As Capture In MatchResults.Groups(1).Captures
        Console.WriteLine("   Capture: {0}", capture.Value)
    Next
    MatchResults = MatchResults.NextMatch()
End While

(I don't know VB.NET, though, so I hope I got the syntax right).

Upvotes: 1

Related Questions