Skip to content

Tiny issue in the regular expression chapter #10

@senarclens

Description

@senarclens

I'm a proud owner of the dead tree edition of your grand book and
believe that the book is really great! I still found a few minor
quirks and checked them against the version here on GIT and most of them were already corrected.
However, there's one left in regular-expressions.html#phonenumbers

The last example in the section is
"""

phonePattern = re.compile(r'''
# don't match beginning of string, number can start anywhere
(\d{3}) # area code is 3 digits (e.g. '800')
\D* # optional separator is any number of non-digits
(\d{3}) # trunk is 3 digits (e.g. '555')
\D* # optional separator
(\d{4}) # rest of number is 4 digits (e.g. '1212')
\D* # optional separator
(\d*) # extension is optional and can be any number of digits
$ # end of string
''', re.VERBOSE)
phonePattern.search('work 1-(800) 555.1212 #1234').groups() ①
('800', '555', '1212', '1234')
phonePattern.search('800-555-1212') ②
('800', '555', '1212', '')
"""

The .groups() is missing in the second numbered line in order to match the
suggested output.

  • expected output:
    ('800', '555', '1212', '')
  • actual output:
    <_sre.SRE_Match at 0x3b52ab0>
  • suggested fix:
    append ".groups()" to last input line

Again - the book is great work and I gladly recommend it to my
students. Thanks for sharing!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions