0

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 
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Yaser Ranjha
  • 107
  • 2
  • 9

1 Answers1

4

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.

RokL
  • 2,663
  • 3
  • 22
  • 26
Guffa
  • 687,336
  • 108
  • 737
  • 1,005