SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Regex - validate URL?

    Regex is definitely not my strong front and I am trying to validate that the input is a valid URL. A search on google yields tons of variations, all of which seem to be very basic and fail a valid url quite easily.

    Does anyone have a reliable pattern to validate a url?

    Appreciate it!
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  2. #2
    SitePoint Zealot j3ph's Avatar
    Join Date
    Jan 2006
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you need to be more specific. What exactly are you trying to validate?

  3. #3
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by j3ph View Post
    I think you need to be more specific. What exactly are you trying to validate?
    seriously?


    Validate that the input is a valid URL in terms of formality, not in terms of "yes the page exists"
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Wrote this a while ago, it is not quiet as strict as the RFC but should handle most use cases. Scheme is required by a URL everything is optional. The Scheme determines what is optional and required.
    Code:
    ~^
    (?:
        (?P<scheme>[-\w.+]+)
        :
        (?://)?
    )
    (?:
        (?:
            (?:
                (?P<user>[^@:]+)
                (?:
                    :
                    (?P<pass>[^@]+)
                )?
            )?
            @
        )?
        (?:
            (?P<host>[^\\/]+?)
            (?:
                :
                (?P<port>\d+)
            )?
        )?
        (?:
            /
            (?P<path>[^?#]+)?
        )?
        (?:
            \?
            (?P<query>[^#]+)
        )?
        (?:
            #
            (?P<fragment>.*?)
        )?
    )
    $~x
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •