开发者

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

开发者 https://www.devze.com 2023-02-08 19:19 出处:网络
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 po

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, a开发者_开发知识库nd 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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消