qckmini6
qckmini6

Reputation: 124

How to regex this?

Hello I'm trying to get a mass type of href into a listbox. Anyone know what's the regex match to get only that thing after href?

Here is a line from multiple ones which I'm trying to optain:

<a class="streamItemsAge" data-hint="January 13, 2016 12:15:31 GMT" href="/Game/player/19928271">yesterday</a>

Here is my code:

        Dim pp As String = textbox1.text
    Dim strRegex As String = "GMT"" <?href\s*=\s*[""'].+?[""'][^>]*?"
    Dim myRegex As New Regex(strRegex, RegexOptions.None)
    For Each myMatch As Match In myRegex.Matches(pp)
        If myMatch.Success Then
            ListBox1.Items.Add(myMatch)
        End If
    Next

Note: data-hint value will be different for each href, so I'm really confused. Please help me.

This will be the output: /Game/player/19928271

Upvotes: 0

Views: 77

Answers (2)

myekem
myekem

Reputation: 139

    Dim pp As String = textbox1.text
    Dim strRegex As String = "data-hint.+href=""([^""]+)"""
    Dim myRegex As New Regex(strRegex, RegexOptions.None)
    For Each myMatch As Match In myRegex.Matches(pp)
        If myMatch.Success Then
            ListBox1.Items.Add(myMatch.Groups(1).Value)
        End If
    Next

Just adjusted Filepe's answer and stuck it in your code. Should do what you want.

Upvotes: 1

Felipe
Felipe

Reputation: 213

The regex below capture the content inside href=""

href="([^"]+)"

Explanation:

  • href=" matches the characters href=" literally
  • () is the capturing group
  • [^"]+ match any character until find the character "
  • " matches the character " literally

You can check it here

Upvotes: 1

Related Questions