Skip to content

Instantly share code, notes, and snippets.

@evaldosantos
Created October 26, 2017 14:12
Show Gist options
  • Save evaldosantos/c3821779e6ce836f5d80d27162d9e488 to your computer and use it in GitHub Desktop.
Save evaldosantos/c3821779e6ce836f5d80d27162d9e488 to your computer and use it in GitHub Desktop.
contacts = re.search(r''' #<-- Wrap in multiline quote. Remove leading caret (^)
(?P<email>[-\w\d.+]+@[-\w\d.]+)
,\s
(?P<phone>\d{3}-\d{3}-\d{4})''' #<-- Add closing paren. Remove extra backslashes. Remove trailing $
, string, re.X | re.M) #<-- Add verbose (for multiline quote), and multiline (for muliple lines in string) flags
re.compile(pattern, flags) - method to pre-compile and save a regular expression pattern, and any associated flags, for later use.
.groupdict() - method to generate a dictionary from a Match object's groups. The keys will be the group names. The values will be the results of the patterns in the group.
re.finditer() - method to generate an iterable from the non-overlapping matches of a regular expression. Very handy for for loops.
.group() - method to access the content of a group. 0 or none is the entire match. 1 through how ever many groups you have will get that group. Or use a group's name to get it if you're using named groups.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment