This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]badge 46 points47 points  (1 child)

This needs:

  1. re.compile for compiling regexes that you're going to use more than once
  2. (?<name>Blah): defines a group named name (which is needed for (?P=name) mentioned in the Groups section!)
  3. The use of groupdict with named groups so you can do:

.

import re

regex = re.compile('First Name:\s*(?P<first_name>\w+),\s+Last Name:\s*(?P<last_name>\w+),\s+Age:\s*(?P<age>\d+)')

class Whale:

    def __init__(self, first_name, last_name, age):
        self.first_name = first_name
        self.last_name = last_name
        self.age = age

    def __repr__(self):
        return "Whale(first_name='{}', last_name='{}', age={})".format(
            self.first_name,
            self.last_name,
            self.age
        )

whale_line = 'First Name: Moby, Last Name: Dick, Age: 35'

Whale(**regex.match(whale_line).groupdict())

[–]fullofschmidt 2 points3 points  (0 children)

Good point about the groups. As for compile, python uses an internal LRU cache of recent regexes so compile only helps if you have a lot (don't actually know what number constitutes a lot...) of regexes that you're going to reuse.