Python Part-13 (Regular Expressions using the Phone number exmaple)

Опубликовано: 09 Октябрь 2024
на канале: Varteta Learning Platform
635
5

Python Regex
In this video I coninued from the previous lesson where we started the example of finding the pattern for a US phone number format (below):
800-555-1212
• 800 555 1212
• 800.555.1212
• (800) 555-1212
• 1-800-555-1212
• 800-555-1212-1234
• 800-555-1212x1234
• 800-555-1212 ext. 1234
• work 1-(800) 555.1212 #1234


Need to import re and then use the search and groups functions of library to create the pattern.

Various switches:

^matches the beginning of a string.
$ matches the end of a string.
\b matches a word boundary.
\d matches any numeric digit.
\D matches any non-numeric character.
x? matches an optional x character (in other words, it matches an x zero or one times).
x* matches x zero or more times.
x+ matches x one or more times
x{n,m} matches an x character at least n times, but not more than m times.
(a|b|c) matches exactly one of a, b or c.
(x) in general is a remembered group. You can get the value of what matched by using the groups() method of the object returned by re.search.

References:
https://docs.python.org/3/howto/regex...

https://www.programiz.com/python-prog...

https://www.google.com/search?source=...