Yaser Ranjha
Yaser Ranjha

Reputation: 107

How To Match Repeated String

I'm very new to StackOverflow

I have a problem:

Dim sample As String = "<b>test string any value </b> <b>This Continue line here </b>"

Dim ra As New Regex("<b>(.*)</b>")

Dim m As Match = ra.Match(sample)
If m.Success Then
   MsgBox(m.Groups(1).Value)
End If

But I got this output:

test string any value </b> <b>This Continue line here 

Upvotes: 0

Views: 85

Answers (1)

Guffa
Guffa

Reputation: 700422

Make the * multiplier non-greedy by adding a question mark after it, to make the expression match as little as possible instead of as much as possible:

Dim ra As New Regex("<b>(.*?)</b>")

When the multiplier is greedy, .* will match everything to the end of the string, then it will backtrack until it finds </b>, which will be the end of the second tag. With a non-greedy multiplier it will start by matching zero characters, then increase the match until it finds </b>, which will be the end of the first tag.

Upvotes: 4

Related Questions