Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5988243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:55:53+00:00 2026-05-22T22:55:53+00:00

I’ve looked all over and have yet to find a single solution to address

  • 0

I’ve looked all over and have yet to find a single solution to address my need for a regular expression pattern that will match a generic URL. I need to support multiple protocols (with verification), localhost and/or IP addressing, ports and query strings. Some examples:

  • http://localhost/mysite
  • https://localhost:55000
  • ftp://192.1.1.1
  • telnet://somesite/page.htm?a=1&b=2

Ideally, I’d like the pattern to also support extracting the various elements (protocol, host, port, query string, etc.) but this is not a requirement.

(Also, for the purposes of myself and future readers, if you could explain the pattern, it would be helpful.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:55:54+00:00Added an answer on May 22, 2026 at 10:55 pm

    Nicholas Carey is correct to steer you towards RFC-3986. The regex he points out will match a generic URI, but it will not validate it (and this regex is not good for picking URLs out of “the wild” – it is too loose and matches just about any string including an empty string).

    Regarding the validation requirement, you may want to take a look at an article I wrote on the subject, which takes from Appendix A all the ABNF syntax definitions of all the various components and provides regex equivalents:

    Regular Expression URI Validation

    Regarding the subject of picking out URL’s from the “wild”, take a look at Jeff Atwood’s “The Problem With URLs” and John’ Gruber’s “An Improved Liberal, Accurate Regex Pattern for Matching URLs” blog posts to get a glimpse as to some of the subtle problems which can arise. Also, you may want to take a look at a project I started last year: URL Linkification – this picks out unlinked HTTP and FTP URLs from text which may already have some links.

    That said, the following is a PHP function which uses a slightly modified version of the RFC-3986 “Absolute URI” regex to validate HTTP and FTP URL’s (with this regex, the named host portion must not be empty). All the various components of the URI are isolated and captured into named groups which allows for easy manipulation and validation of the parts within the program code:

    function url_valid($url)
    {
        if (strpos($url, 'www.') === 0) $url = 'http://'. $url;
        if (strpos($url, 'ftp.') === 0) $url = 'ftp://'. $url;
        if (!preg_match('/# Valid absolute URI having a non-empty, valid DNS host.
            ^
            (?P<scheme>[A-Za-z][A-Za-z0-9+\-.]*):\/\/
            (?P<authority>
              (?:(?P<userinfo>(?:[A-Za-z0-9\-._~!$&\'()*+,;=:]|%[0-9A-Fa-f]{2})*)@)?
              (?P<host>
                (?P<IP_literal>
                  \[
                  (?:
                    (?P<IPV6address>
                      (?:                                                (?:[0-9A-Fa-f]{1,4}:){6}
                      |                                                ::(?:[0-9A-Fa-f]{1,4}:){5}
                      | (?:                          [0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){4}
                      | (?:(?:[0-9A-Fa-f]{1,4}:){0,1}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){3}
                      | (?:(?:[0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})?::(?:[0-9A-Fa-f]{1,4}:){2}
                      | (?:(?:[0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})?::   [0-9A-Fa-f]{1,4}:
                      | (?:(?:[0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})?::
                      )
                      (?P<ls32>[0-9A-Fa-f]{1,4}:[0-9A-Fa-f]{1,4}
                      | (?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
                           (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)
                      )
                    |   (?:(?:[0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})?::   [0-9A-Fa-f]{1,4}
                    |   (?:(?:[0-9A-Fa-f]{1,4}:){0,6}[0-9A-Fa-f]{1,4})?::
                    )
                  | (?P<IPvFuture>[Vv][0-9A-Fa-f]+\.[A-Za-z0-9\-._~!$&\'()*+,;=:]+)
                  )
                  \]
                )
              | (?P<IPv4address>(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}
                                   (?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))
              | (?P<regname>(?:[A-Za-z0-9\-._~!$&\'()*+,;=]|%[0-9A-Fa-f]{2})+)
              )
              (?::(?P<port>[0-9]*))?
            )
            (?P<path_abempty>(?:\/(?:[A-Za-z0-9\-._~!$&\'()*+,;=:@]|%[0-9A-Fa-f]{2})*)*)
            (?:\?(?P<query>       (?:[A-Za-z0-9\-._~!$&\'()*+,;=:@\\/?]|%[0-9A-Fa-f]{2})*))?
            (?:\#(?P<fragment>    (?:[A-Za-z0-9\-._~!$&\'()*+,;=:@\\/?]|%[0-9A-Fa-f]{2})*))?
            $
            /mx', $url, $m)) return FALSE;
        switch ($m['scheme'])
        {
        case 'https':
        case 'http':
            if ($m['userinfo']) return FALSE; // HTTP scheme does not allow userinfo.
            break;
        case 'ftps':
        case 'ftp':
            break;
        default:
            return FALSE;   // Unrecognised URI scheme. Default to FALSE.
        }
        // Validate host name conforms to DNS "dot-separated-parts".
        if ($m{'regname'}) // If host regname specified, check for DNS conformance.
        {
            if (!preg_match('/# HTTP DNS host name.
                ^                      # Anchor to beginning of string.
                (?!.{256})             # Overall host length is less than 256 chars.
                (?:                    # Group dot separated host part alternatives.
                  [0-9A-Za-z]\.        # Either a single alphanum followed by dot
                |                      # or... part has more than one char (63 chars max).
                  [0-9A-Za-z]          # Part first char is alphanum (no dash).
                  [\-0-9A-Za-z]{0,61}  # Internal chars are alphanum plus dash.
                  [0-9A-Za-z]          # Part last char is alphanum (no dash).
                  \.                   # Each part followed by literal dot.
                )*                     # One or more parts before top level domain.
                (?:                    # Explicitly specify top level domains.
                  com|edu|gov|int|mil|net|org|biz|
                  info|name|pro|aero|coop|museum|
                  asia|cat|jobs|mobi|tel|travel|
                  [A-Za-z]{2})         # Country codes are exqactly two alpha chars.
                $                      # Anchor to end of string.
                /ix', $m['host'])) return FALSE;
        }
        $m['url'] = $url;
        for ($i = 0; isset($m[$i]); ++$i) unset($m[$i]);
        return $m; // return TRUE == array of useful named $matches plus the valid $url.
    }
    

    The first regex validates the string as an absolute (has a non-empty host portion) generic URI. A second regex is used to validate the (named) host portion (when it is not an IP literal or IPv4 address) with regard to the DNS lookup system (where each dot-separated subdomain is 63 chars or less consisting of digits, letters and dashes, with an overall length less than 255 chars.)

    Note that the structure of this function allows easy expansion to include other schemes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I need a function that will clean a strings' special characters. I do NOT
I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.