Stefan Đorđević Stefan Đorđević - 2 years ago 175
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">

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)

Ok, so this code works pretty well....with
. I tried to change pattern1 and pattern2's
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 Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download