3

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

this is what i currently have:

((https?\:\/\/)?([^\s\.\-]{1,}(?:(?:\.|\-)[^\s\.\-]{1,}){0,})(?=\.(?:[^\s]{1,}){0,2}\/|$)(\.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|\.pg|\.ph|\.pk|\.pl|\.pm|\.pn|\.pr|\.ps|\.pt|\.pw|\.py|\.qa|\.re|\.ro|\.rs|\.ru|\.rw|\.sa|\.sb|\.sc|\.sd|\.se|\.sg|\.sh|\.si|\.sk|\.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)(\/[^\s]{0,})?)

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

JvdV
  • 70,606
  • 8
  • 39
  • 70
Alan
  • 949
  • 8
  • 26

2 Answers2

1

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)),MID(A2,SEARCH("=",SUBSTITUTE(A2,".","=",LEN(A2)-LEN(SUBSTITUTE(A2,".",""))))+1,256))>0,"Website","Other")

Or

=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:

=WEBSITE(A2)
JvdV
  • 70,606
  • 8
  • 39
  • 70
  • 1
    upvoted. gonna wait before making this the accepted answer to see if anyone else got a better solution. the funny thing is i was working on a formula based solution aswell, but i thought using REGEX would be a more precise way of solving this problem – Alan Jun 19 '19 at 09:50
  • @Alan, thanks, I also updated the answer a little to show how you can do this without REGEX – JvdV Jun 19 '19 at 11:07
1

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

Michał Turczyn
  • 32,028
  • 14
  • 47
  • 69
  • ok thanks, this helped for now. i updated the question, since i feel like my current solution is not effective – Alan Jun 19 '19 at 10:11