Why would you want to compile a pattern with compile() ?

  1. It’s going to be used multiple times
  2. I want to be able to pass it to functions
  3. I want to be able to use it directly
  4. I want to provide multiple patterns as part of a library
import re

names_file = open("names.txt", encoding="utf-8")
data = names_file.read()
names_file.close()

line = re.compile(r'''
    ^(?P<name>(?P<last>[-\w ]*),\s(?P<first>[-\w ]+))\t  # Grouped last and first names
    (?P<email>[-\w\d.+]+@[-\w\d.]+)\t                    # Email
    (?P<phone>\(?\d{3}\)?-?\s?\d{3}-\d{4})?\t            # Phone
    (?P<job>[\w\s]+,\s[\w\s.]+)\t?                       # Job and company
    (?P<twitter>@[\w\d]+)?$                              # Twitter
    ''', re.X|re.M)

# A new way to search a compiled match object with a string.
line.search(data).groupdict()

for match in line.finditer(data):
    print('{first} {last} <{email}>'.format(**match.groupdict()))

.finditer()
A method that gives us an iterable full of match objects.


Exercise

Create a variable named players that is an re.search() or re.match() to capture three groups: last_name, first_name, and score. It should include re.MULTILINE.

Then, create a class named Player that has those same three attributes, last_name, first_name, and score. I should be able to set them through __init__.

import re

string = '''Love, Kenneth: 20
Chalkley, Andrew: 25
McFarland, Dave: 10
Kesten, Joy: 22
Stewart Pinchback, Pinckney Benton: 18'''

players = re.search(r''' 
        ^(?P<last_name>[\w\s]+),\s(?P<first_name>[\w\s]+):\s
        (?P<score>[\d]+)$
        ''', string, re.X|re.M)

players_dict = players.groupdict()

class Player():
    def __init__(self, **players_dict):
        self.last_name = players_dict.get('last_name', ' ')
        self.first_name = players_dict.get('first_name', ' ')
        self.score = players_dict.get('score', ' ')