1. Learn
  2. /
  3. Courses
  4. /
  5. Natural Language Processing with spaCy

Connected

Exercise

RegEx in Python

Rule-based information extraction is useful for many NLP tasks. Certain types of entities, such as dates or phone numbers have distinct formats that can be recognized by a set of rules without needing to train any model. In this exercise, you will practice using re package for RegEx. The goal is to find phone numbers in a given text.

re package is already imported for your use. You can use \d to match string patterns representative of a metacharacter that matches any digit from 0 to 9.

Instructions

100 XP
  • Define a pattern to match phone numbers of the form (111)-111-1111.
  • Find all the matching patterns using re.finditer() method.
  • For each match, print start and end characters and matching section of the given text.