Home » excel » Regex – URL Regex SPECIFICALLY for Excel VBA

Regex – URL Regex SPECIFICALLY for Excel VBA

Posted by: admin May 14, 2020 Leave a comment


Im trying to come up with my own variant of an url regex to use in vba

this is what i currently have:


Currently I’m trying to match specific domain endings, because i want to exclude mobile app names (com.king.candycrushsodasaga should NOT be included for instance)
It would be very good however, if i could use a more generic regex to achieve this goal since manually putting all those domain endings is not very effective/efficient

If there is a better way of doing so please let me know.

Appreciate any help.

Additional Info:
I’m trying to use this for a excel, where i can drop a bunch of urls, including mobile apps (like com.king.candycrushsodasaga) into a table and match the actual websites in a different column to exclude non websites like mobile apps.

This is what the table looks like:

The table looks something like this

more background info:

i already have a vba function, which can be used as a formula.
it takes in 2 arguments, one beeing the cell/range in which the url is and one beeing a range, in which the regex is.
for some reason long strings throw “#value” so i had to split some regexes.

this is what the formula looks like:

=IF(IsMatch([@Url];RegularExps[URL Regex 1]);"Website";"Other")

I already tried to use regex’es (or regexi, whatever the plural for regex is) from this post: What is the best regular expression to check if a string is a valid URL?

But i havent been successful with any of them as they either include the app domains, throw #value or exclude valid urls

How to&Answers:

Just add to your huge alternation \.com like (...|\.com...), before co, as it will successfully match leaving out m and not checking com.


I know you are asking about regex, but I am not sure if it would be that userfriendly. Here is an example with a lookup table:

enter image description here

Formula in B2:



=IF(COUNTIF($E$2:INDEX(E:E,COUNTA(E:E)),TRIM(RIGHT(SUBSTITUTE(A2,".",REPT(" ",LEN(A1))),LEN(A1))))>0,"Website","Other")

I simply added .com to the list. Add to the list if you like, the range is dynamic. You could also make it a table offcourse and reference that.

If you choose to use VBA, I don’t know what the added value of REGEX would be. There are other ways, but indeed REGEX would be one of them. For example you could use:

Function WEBSITE(RNG As Range) As String

Select Case Evaluate("Trim(Right(Substitute(" & RNG.Address & ", ""."", Rept("" "", Len(" & RNG.Address & "))), Len(" & RNG.Address & ")))")
Case "ac", "ad", "ae", "af", "ag", "ai", "al", "am", "ao", "aq", "ar", _
    "as", "at", "au", "aw", "ax", "az", "ba", "bb", "bd", "be", "bf", "bg", _
    "bh", "bi", "bj", "bm", "bn", "bo", "br", "bs", "bt", "bw", "by", "bz", _
    "ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl", "cm", "cn", "co", _
    "cr", "cu", "cv", "cw", "cx", "cy", "cz", "de", "dj", "dk", "dm", "do", _
    "dz", "ec", "ee", "eg", "es", "et", "eu", "fi", "fj", "fk", "fm", "fo", _
    "fr", "ga", "gd", "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", _
    "gq", "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr", "ht", _
    "hu", "id", "ie", "il", "im", "in", "io", "iq", "ir", "is", "it", "je", _
    "jm", "jo", "jp", "ke", "kg", "kh", "ki", "km", "kn", "kp", "kr", "kw", _
    "ky", "kz", "la", "lb", "lc", "li", "lk", "lr", "ls", "lt", "lu", "lv", _
    "ly", "ma", "mc", "md", "me", "mg", "mh", "mk", "ml", "mm", "mn", "mo", _
    "mp", "mq", "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na", _
    "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu", "nz", "om", _
    "pa", "pe", "pf", "sl", "sm", "sn", "so", "sr", "ss", "st", "su", "sv", _
    "sx", "sy", "sz", "tc", "td", "tf", "tg", "th", "tj", "tk", "tl", "tm", _
    "tn", "to", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "uk", "us", "uy", _
    "uz", "va", "vc", "ve", "vg", "vi", "vn", "vu", "wf", "ws", "ye", "yt", _
    "za", "zm", "zw", "com"

    WEBSITE = "Website"
Case Else
    WEBSITE = "Other"
End Select

End Function

Call like: