Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you validate a URL with a regular expression in Python?

I'm building an app on Google App Engine. I'm incredibly new to Python and have been beating my head against the following problem for the past 3 days.

I have a class to represent an RSS Feed and in this class I have a method called setUrl. Input to this method is a URL.

I'm trying to use the re python module to validate off of the RFC 3986 Reg-ex (http://www.ietf.org/rfc/rfc3986.txt)

Below is a snipped which should work?

p = re.compile('^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?') m = p.match(url) if m:   self.url = url   return url 
like image 740
Zee Spencer Avatar asked May 06 '09 00:05

Zee Spencer


People also ask

How do you check if a URL is valid in Python?

To check whether the string entered is a valid URL or not we use the validators module in Python. When we pass the string to the method url() present in the module it returns true(if the string is URL) and ValidationFailure(func=url, …) if URL is invalid.

How do you check if a URL is valid?

You can use the URLConstructor to check if a string is a valid URL. URLConstructor ( new URL(url) ) returns a newly created URL object defined by the URL parameters. A JavaScript TypeError exception is thrown if the given URL is not valid.

How do you match a URL in Python?

To find the URLs in a given string we have used the findall() function from the regular expression module of Python. This return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found.

How do you validate an expression in Python?

fullmatch(). This method checks if the whole string matches the regular expression pattern or not. If it does then it returns 1, otherwise a 0.


1 Answers

Here's the complete regexp to parse a URL.

(?:http://(?:(?:(?:(?:(?:[a-zA-Z\d](?:(?:[a-zA-Z\d]|-)*[a-zA-Z\d])?)\. )*(?:[a-zA-Z](?:(?:[a-zA-Z\d]|-)*[a-zA-Z\d])?))|(?:(?:\d+)(?:\.(?:\d+) ){3}))(?::(?:\d+))?)(?:/(?:(?:(?:(?:[a-zA-Z\d$\-_.+!*'(),]|(?:%[a-fA-F \d]{2}))|[;:@&=])*)(?:/(?:(?:(?:[a-zA-Z\d$\-_.+!*'(),]|(?:%[a-fA-F\d]{ 2}))|[;:@&=])*))*)(?:\?(?:(?:(?:[a-zA-Z\d$\-_.+!*'(),]|(?:%[a-fA-F\d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?:/;[Uu][Ii][Dd]=(?:[1-9]\d*))(?:(?:/;[Ss][Ee][Cc][Tt][Ii][Oo][Nn ]=(?:(?:(?:[a-zA-Z\d$\-_.+!*'(),]|(?:%[a-fA-F\d]{2}))|[&=~:@/])+)))?)) )?)|(?:nfs:(?:(?://(?:(?:(?:(?:(?:[a-zA-Z\d](?:(?:[a-zA-Z\d]|-)*[a-zA- Z\d])?)\.)*(?:[a-zA-Z](?:(?:[a-zA-Z\d]|-)*[a-zA-Z\d])?))|(?:(?:\d+)(?: \.(?:\d+)){3}))(?::(?:\d+))?)(?:(?:/(?:(?:(?:(?:(?:[a-zA-Z\d\$\-_.!~*' (),])|(?:%[a-fA-F\d]{2})|[:@&=+])*)(?:/(?:(?:(?:[a-zA-Z\d\$\-_.!~*'(), ])|(?:%[a-fA-F\d]{2})|[:@&=+])*))*)?)))?)|(?:/(?:(?:(?:(?:(?:[a-zA-Z\d \$\-_.!~*'(),])|(?:%[a-fA-F\d]{2})|[:@&=+])*)(?:/(?:(?:(?:[a-zA-Z\d\$\ -_.!~*'(),])|(?:%[a-fA-F\d]{2})|[:@&=+])*))*)?))|(?:(?:(?:(?:(?:[a-zA- Z\d\$\-_.!~*'(),])|(?:%[a-fA-F\d]{2})|[:@&=+])*)(?:/(?:(?:(?:[a-zA-Z\d \$\-_.!~*'(),])|(?:%[a-fA-F\d]{2})|[:@&=+])*))*)?))) 

Given its complexibility, I think you should go the urlparse way.

For completeness, here's the pseudo-BNF of the above regex (as a documentation):

; The generic form of a URL is:  genericurl     = scheme ":" schemepart  ; Specific predefined schemes are defined here; new schemes ; may be registered with IANA  url            = httpurl | ftpurl | newsurl |                  nntpurl | telneturl | gopherurl |                  waisurl | mailtourl | fileurl |                  prosperourl | otherurl  ; new schemes follow the general syntax otherurl       = genericurl  ; the scheme is in lower case; interpreters should use case-ignore scheme         = 1*[ lowalpha | digit | "+" | "-" | "." ] schemepart     = *xchar | ip-schemepart   ; URL schemeparts for ip based protocols:  ip-schemepart  = "//" login [ "/" urlpath ]  login          = [ user [ ":" password ] "@" ] hostport hostport       = host [ ":" port ] host           = hostname | hostnumber hostname       = *[ domainlabel "." ] toplabel domainlabel    = alphadigit | alphadigit *[ alphadigit | "-" ] alphadigit toplabel       = alpha | alpha *[ alphadigit | "-" ] alphadigit alphadigit     = alpha | digit hostnumber     = digits "." digits "." digits "." digits port           = digits user           = *[ uchar | ";" | "?" | "&" | "=" ] password       = *[ uchar | ";" | "?" | "&" | "=" ] urlpath        = *xchar    ; depends on protocol see section 3.1  ; The predefined schemes:  ; FTP (see also RFC959)  ftpurl         = "ftp://" login [ "/" fpath [ ";type=" ftptype ]] fpath          = fsegment *[ "/" fsegment ] fsegment       = *[ uchar | "?" | ":" | "@" | "&" | "=" ] ftptype        = "A" | "I" | "D" | "a" | "i" | "d"  ; FILE  fileurl        = "file://" [ host | "localhost" ] "/" fpath  ; HTTP  httpurl        = "http://" hostport [ "/" hpath [ "?" search ]] hpath          = hsegment *[ "/" hsegment ] hsegment       = *[ uchar | ";" | ":" | "@" | "&" | "=" ] search         = *[ uchar | ";" | ":" | "@" | "&" | "=" ]  ; GOPHER (see also RFC1436)  gopherurl      = "gopher://" hostport [ / [ gtype [ selector                  [ "%09" search [ "%09" gopher+_string ] ] ] ] ] gtype          = xchar selector       = *xchar gopher+_string = *xchar  ; MAILTO (see also RFC822)  mailtourl      = "mailto:" encoded822addr encoded822addr = 1*xchar               ; further defined in RFC822  ; NEWS (see also RFC1036)  newsurl        = "news:" grouppart grouppart      = "*" | group | article group          = alpha *[ alpha | digit | "-" | "." | "+" | "_" ] article        = 1*[ uchar | ";" | "/" | "?" | ":" | "&" | "=" ] "@" host  ; NNTP (see also RFC977)  nntpurl        = "nntp://" hostport "/" group [ "/" digits ]  ; TELNET  telneturl      = "telnet://" login [ "/" ]  ; WAIS (see also RFC1625)  waisurl        = waisdatabase | waisindex | waisdoc waisdatabase   = "wais://" hostport "/" database waisindex      = "wais://" hostport "/" database "?" search waisdoc        = "wais://" hostport "/" database "/" wtype "/" wpath database       = *uchar wtype          = *uchar wpath          = *uchar  ; PROSPERO  prosperourl    = "prospero://" hostport "/" ppath *[ fieldspec ] ppath          = psegment *[ "/" psegment ] psegment       = *[ uchar | "?" | ":" | "@" | "&" | "=" ] fieldspec      = ";" fieldname "=" fieldvalue fieldname      = *[ uchar | "?" | ":" | "@" | "&" ] fieldvalue     = *[ uchar | "?" | ":" | "@" | "&" ]  ; Miscellaneous definitions  lowalpha       = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" |                  "i" | "j" | "k" | "l" | "m" | "n" | "o" | "p" |                  "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" |                  "y" | "z" hialpha        = "A" | "B" | "C" | "D" | "E" | "F" | "G" | "H" | "I" |                  "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" |                  "S" | "T" | "U" | "V" | "W" | "X" | "Y" | "Z" alpha          = lowalpha | hialpha digit          = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" |                  "8" | "9" safe           = "$" | "-" | "_" | "." | "+" extra          = "!" | "*" | "'" | "(" | ")" | "," national       = "{" | "}" | "|" | "\" | "^" | "~" | "[" | "]" | "`" punctuation    = "" | "#" | "%" |    reserved       = ";" | "/" | "?" | ":" | "@" | "&" | "=" hex            = digit | "A" | "B" | "C" | "D" | "E" | "F" |                  "a" | "b" | "c" | "d" | "e" | "f" escape         = "%" hex hex  unreserved     = alpha | digit | safe | extra uchar          = unreserved | escape xchar          = unreserved | reserved | escape digits         = 1*digit 
like image 57
nosklo Avatar answered Sep 28 '22 02:09

nosklo