none
How to capture text inside <TAGs> , BUT by excluding the tags from result? RRS feed

  • Question

  • In Visual Basic I am trying to get text between tags but TAGS are also captured.

        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</tag> Other Text"
    
            Dim capture As Match = Regex.Match(text, pattern)
    
            MsgBox(capture.Value)
    
        End Sub

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

    But result is coming together with tags at both sides.

    What is wrong in code?


    Friday, October 27, 2017 2:56 PM

All replies

  • In your example is text real data or is it representative of the real data?  If it is the latter what is the real data?

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it" - MSDN User JohnWein

    Friday, October 27, 2017 3:36 PM
  • Hi! :) This kind of links well to my current project, otherwise I'd have no clue. Mabye try this? Probably wrong but worth a shot:

    strOutput = Regex.Replace(strOutput, ">", "")
    strOutput = Regex.Replace(strOutput, "<", "")
    'Basically replaces the tags with the what the second pair of quotation marks contain (nothing)

    Hope this helps :)

    Friday, October 27, 2017 3:53 PM
  • Try this:

       MsgBox(capture.Groups(1).Value)

    or this:

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

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

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

       MsgBox(match.Value)


    Friday, October 27, 2017 4:20 PM
  • Use the 2nd Group value in this line....

    MsgBox(capture.Groups(1).Value)


    If you say it can`t be done then i`ll try it

    Friday, October 27, 2017 8:29 PM
  • Try this:

       MsgBox(capture.Groups(1).Value)

    or this:

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

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

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

       MsgBox(match.Value)


    Yes, Groups(1) is the solution..Thank you very much.
    Saturday, October 28, 2017 10:52 AM
  • Hi Samlays,

    It seems that Viorel_'s post can resolve your issue, please remember to close your thread by marking his post as answer, it is beneficial to other community members who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, October 30, 2017 1:17 AM
    Moderator