user1785594
user1785594

Reputation:

regex value to string

I wish to use regex to match a link and put the first one into the string.

I don't know why below code only show nothing.

Please help me.

Private Sub Info_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Info.Click

        dim theusercp as string = "http://google.com/abcd/12345"

        Dim pattern As String = "http://.*.com"
        Dim rs As String = Regex.Match(theusercp, pattern, RegexOptions.Multiline).Groups(1).Value


        urlbase.Text = rs
'Display the string in label1
        MessageBox.Show(rs)
'I hoped it to show "http://google.com" but the result is blank.

    End Sub

Upvotes: 1

Views: 25

Answers (1)

Heinzi
Heinzi

Reputation: 172200

In your code, you refer to Groups(1), but there are no groups in your regular expression! Just return the matched value directly:

Dim rs As String = Regex.Match(theusercp, pattern, RegexOptions.Multiline).Value

This will yield the desired output.

As a side note: Your pattern will also match http://example.com/xcom in http://example.com/xcom/123. I'm not sure this is what you indended...

Upvotes: 0

Related Questions