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 
Yaser Ranjha
  • 107
  • 2
  • 8

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,748
  • 3
  • 21
  • 23
Guffa
  • 666,277
  • 106
  • 705
  • 986