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

Posted by Zachary Spencer on Stack Overflow See other posts from Stack Overflow or by Zachary Spencer
Published on 2009-05-06T00:40:30Z Indexed on 2010/03/12 8:57 UTC
Read the original article Hit count: 221

Filed under:
|
|

I'm building a Google App Engine app, and I have a class to represent an RSS Feed.

I have a method called setUrl which is part of the feed class. It accepts a url as an input.

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, right? I'm incredibly new to Python and have been beating my head against this for the past 3 days.

p = re.compile('^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?')
m = p.match(url)
if m:
  self.url = url
  return url

© Stack Overflow or respective owner

Related posts about python

Related posts about google-app-engine