Stefan Đorđević Stefan Đorđević - 5 months ago 57
HTML Question

Get text between two strings Regex VB.Net

I really have serious problems with regex. I need to get all text between 2 strings, in this case that strings are

<span class="user user-role-registered-member">
and
</span>
.

I googled pretty much questions (some of them are on StackOverFlow), and watched YouTube tutorials, still can't get it.

This is the code that i think would work, but i don't know why it doesn't.

Dim mystring As String = "<br>Terms of Service<br></br>Developers<br>"

Dim pattern1 As String = "(?<=<br>)(.*?)(?=<br>)"
Dim pattern2 As String = "(?<=</br>)(.*)(?=<br>)"

Dim m1 As MatchCollection = Regex.Matches(mystring, pattern1)
Dim m2 As MatchCollection = Regex.Matches(mystring, pattern2)
MsgBox(m1(0).ToString)
MsgBox(m2(0).ToString)


Ok, so this code works pretty well....with
<br>
. I tried to change pattern1 and pattern2's
<br>
with span but it doesn't work. I know that i am making a mistake here, but i don't know where/how.

Any answer will be really helpful.

Answer

This does the job easily and beautifully. It won't return a match when there is no text inside the span, so you do not need to worry about empty matches. It will however return groups with only whitespace in them.

<span class=""user user-role-registered-member"">(.+)</span>

Test it out here.

Comments