Click here to Skip to main content
14,236,822 members
Rate this:
Please Sign up or sign in to vote.
See more:
In Visual Basic I am trying to get text between tags but TAGS are also captured.

I want to see the result as ONLY "Captured Text" in message box.

But result is coming as "Captured Text" with both tags before and after .



What is wrong in my code?

What I have tried:

Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim pattern As String = "<tag>(.+?)<\/tag>"

Dim text As String = "Other Text <tag>Captured Text Other Text"

Dim capture As Match = Regex.Match(text, pattern)

MsgBox(capture.Value)

End Sub
Posted
Updated 28-Oct-17 0:56am
v2
Rate this:
Please Sign up or sign in to vote.

Solution 1

A regex captures the text you told it to - which in your case includes teh tags.
You can use an non-capturing prefix and suffix:
(?<=<tag>)(.+?)(?=<\/tag>)
But if you are trying to process HTML, I'd recommend a proper parser instead: Html Agility Pack | HAP[^] is a good start.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

I found the solution

I must use Groups(1) for getting only text inside tags ..This is the solution..
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100