Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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.
 
Share this answer
 
I found the solution

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900