Python Regular Expressions
1. Extracting a single url OR all the urls in a text snippet
If you're only looking for one:
import re match = re.search(r'href=[\'"]?([^\'" >]+)', s) if match: print match.group(0)
If you have a long string, and want every instance of the pattern in it:
import re urls = re.findall(r'href=[\'"]?([^\'" >]+)', s) print ', '.join(urls)
Where
sis the string that you're looking for matches in.
Compilation Flags -- inline
Python
For example, the following pattern ignores case (case insensitive):
re.search ("(?i)password", string)
The flags can be combined. The following ignores case and matches DOTALL:
re.search ("(?is)username.*?password", string)
Parse a simple date
This format is used by the `motion` command. This parses the date from filenames in this example format:
02-20070505085051-04.jpg
The return format is a sequence the same as used in the standard Python library time module. This is pretty trivial, but shows using named sub-expressions.
def date_from_filename (filename): m = re.match(".*?[0-9]{2}-(?P
[0-9]{4})(?P [0-9]{2})(?P [0-9]{2})(?P [0-9]{2})(?P [0-9]{2})(?P [0-9]{2})-(?P [0-9]{2}).*?", filename) if m is None: print "Bad date parse in filename:", filename return None day = int(m.group('DAY')) month = int(m.group('MONTH')) year = int(m.group('YEAR')) hour = int(m.group('HOUR')) min = int(m.group('MIN')) sec = int(m.group('SEC')) dts = (year, month, day, hour, min, sec, 0, 1, -1) return dts
Common Short RegEx Patterns
These are some regex patterns that I have found come up often. These examples assume a string, called page, that could, for instance, be an HTML document downloaded from the web. They will return a list of all non-overlapping matches found on the page.
import urllib, re page = ''.join( urllib.urlopen('').readlines() )
It's almost impossible to match a legal email address with a small, concise regex, but this one will match most.
URL regex pattern
According to RFC 1808, the class [!*\(\),] should really be [!*'\(\),], but single quotes are always meta-quoted in HTML files, so if I included the quote I would get extra characters in matches.
urls = re.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', page)
IP address regex pattern
ips = re.findall('(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})\.(?:[\d]{1,3})', page)