Skip to content

Instantly share code, notes, and snippets.

@metaodi
Last active February 5, 2016 12:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metaodi/3116956fef7d4a7bc80a to your computer and use it in GitHub Desktop.
Save metaodi/3116956fef7d4a7bc80a to your computer and use it in GitHub Desktop.
Python Summit 2016
# Named regex groups
import re
sentence = "\"The Hitchhiker's Guide to the Galaxy\" was published in 1979"
regex = "\"([\w ']+)\" was published in (\S+)"
print "find_all: %s"%(re.findall(regex, sentence))
match = re.search("\"(?P<book>[\w ']+)\" was published in (?P<year>\S+)", sentence)
print "match groups: %s"%(str(match.groups()))
print "match group1: %s"%(str(match.group(1)))
print "match group1 span: %s"%(str(match.span(1)))
print "match groupdict: %s"%(str(match.groupdict()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment