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.
Diese Übung ist Teil des Kurses
Natural Language Processing with spaCy
Anleitung zur Übung
- 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
.
Interaktive Übung
Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.
text = "Our phone number is (425)-123-4567."
# Define a pattern to match phone numbers
pattern = r"\((____){____}\)-(____){____}-(____){____}"
# Find all the matching patterns in the text
phones = re.____(pattern, text)
# Print start and end characters and matching section of the text
for match in phones:
start_char = match.____
end_char = match.____
print("Start character: ", ____, "| End character: ", ____, "| Matching text: ", text[____:____])