-
-
Save incidunt/85fc417ae1fb59af9c6de2e4ac8f01a3 to your computer and use it in GitHub Desktop.
John Gruber's regex to find URLs in plain text, converted to Python/Unicode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#See: http://daringfireball.net/2010/07/improved_regex_for_matching_urls | |
import re, urllib | |
GRUBER_URLINTEXT_PAT = re.compile(ur'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))') | |
for line in urllib.urlopen("http://daringfireball.net/misc/2010/07/url-matching-regex-test-data.text"): | |
print [ mgroups[0] for mgroups in GRUBER_URLINTEXT_PAT.findall(line) ] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment