hankcoder has asked for the wisdom of the Perl Monks concerning the following question:

Just to share with you all of my findings and feel free to comment as I'm no expert in this regex so I just use whichever I feel can solve my problem.

After spending hours of research to use regex and even Data::Validate::URI, finally I have encounter this website list with lots of regex to check if url is valid or not. (https://mathiasbynens.be/demo/url-regex)

I have already picked one that best suits my needs.

Feel free to comment if there are any cons of using such regex.

Replies are listed 'Best First'.
Re: Regex check Valid URL
by GrandFather (Saint) on Aug 17, 2015 at 10:43 UTC

    You may find URI helpful. Note the regex given in the "PARSING URIs WITH REGEXP" section.

    Premature optimization is the root of all job security
Re: Regex check Valid URL
by vinoth.ree (Monsignor) on Aug 17, 2015 at 08:13 UTC
    I have already picked one that best suits my needs

    If you already chosen the best one, what do you want to discuss more?

    what is the URL you are trying to validate?

    what is the best Regex you chosen?


    All is well. I learn by answering your questions...

      vinoth.ree, the said regex may not serve you any good but it may serve someone else. Furthermore, as to my knowledge, there are too many variant of url to validate for just one single prefect regex. And I am not looking for perfect regex.

      The previous regex I tested which I found on public internet are not able to serve my requirement.

      ^(http(?:s)?\:\/\/[a-zA-Z0-9]+(?:(?:\.|\-)[a-zA-Z0-9]+)+(?:\:\d+)?(?:\ +/[\w\-]+)*(?:\/?|\/\w+\.[a-zA-Z]{2,4}(?:\?[\w]+\=[\w\-]+)?)?(?:\&[\w] ++\=[\w\-]+)*)$
      Test URL:<br> http://www.abc.com/test.pl?a=1234 http://www.abc.com/?a=1234

      The above regex failed on 2nd url which it should be valid as well. My current successful tested regex that works for my need are:

      # @stephenhay (38 chars) ^(https?|ftp)://[^\s/$.?#].[^\s]*$ # @imme_emosol (54 chars) (https?|ftp)://(-\.)?([^\s/?\.#-]+\.?)+(/[^\s]*)?$

      There isn't a need to discuss further if you feel there isn't a need to.

      Perhaps this post should be in Meditations instead.