1

I'm trying to get the domain name from a string. From this URL http://www.tvstore.nl/product/572875/samsung-ue48ju7500.html

I'd like to get www.tvstore.nl as a result. However, with this function I just get www.tvstore. What am I missing? I want this to work for any TLD, whether that's .com, .org, .de, .co etc.

Protected Function ExtractDomainFromURL(ByVal sURL As String) As String
    Dim rg As New Regex("://(?<host>([a-z\d][-a-z\d]*[a-z\d]\.)*[a-z][-a-z\d]+[a-z])")
    If rg.IsMatch(sURL) Then
        Return rg.Match(sURL).Result("${host}")
    Else
        Return String.Empty
    End If
End Function

1 Answer 1

1
:\/\/(?<host>([a-z\d][-a-z\d]*[a-z\d]\.)*[a-z][-a-z\d]*[a-z])

                                                     ^^

Just change the quantifier from + to *.As you have used + it is expecting a 3 letter word ,whereas you have only 2 i.e nl.See demo.

https://regex101.com/r/iV6mP5/3

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.