Podcast
Questions and Answers
Which function is used to create Regex objects?
Which function is used to create Regex objects?
Raw strings are not necessary when creating Regex objects.
Raw strings are not necessary when creating Regex objects.
False (B)
What method would you use to find all occurrences of a pattern in a string?
What method would you use to find all occurrences of a pattern in a string?
findall()
In the regex pattern r'( ext{test})', the '\b' denotes a _______.
In the regex pattern r'( ext{test})', the '\b' denotes a _______.
Signup and view all the answers
Match the following regex characters with their meanings:
Match the following regex characters with their meanings:
Signup and view all the answers
What does the | character signify in regular expressions?
What does the | character signify in regular expressions?
Signup and view all the answers
The .* pattern matches any character except newline by default.
The .* pattern matches any character except newline by default.
Signup and view all the answers
How can you make a regex search case-insensitive?
How can you make a regex search case-insensitive?
Signup and view all the answers
Study Notes
Regular Expression Functions and Methods
-
re.compile()
creates Regex objects. - Raw strings (prefixed with 'r') are often used for Regex patterns to avoid backslash escapes.
-
search()
returns a Match object if a match is found; otherwise, it returns None. - To get the matched strings from a Match object, use the
group()
method. For instance,match.group(0)
returns the entire match,match.group(1)
returns the first captured group, and so on.
Regex Specifics with Examples
-
r'(\d\d\d)-(\d\d\d-\d\d\d\d)'
: This regex matches a phone number with the format ###-###-####.-
group(0)
covers the entire match (e.g., '123-456-7890'). -
group(1)
covers the first set of three digits (e.g., '123'). -
group(2)
covers the second set of three digits followed by four digits (e.g., '456-7890').
-
- To match literal parentheses or periods, escape them with a backslash (e.g.,
\(
,\)
or\.
) -
findall()
returns a list of strings if the regex doesn't contain capturing groups, or a list of tuples of strings if capturing groups are present. -
|
signifies OR (alternation) within a regular expression -
?
signifies zero or one occurrences of the preceding element, or non-greedy match -
+
signifies one or more occurrences of the preceding element. -
*
signifies zero or more occurrences of the preceding element. -
{3}
signifies exactly three occurrences of the preceding element. -
{3,5}
signifies between three and five occurrences of the preceding element. -
\d
matches any digit (0-9) -
\w
matches any alphanumeric character (a-z, A-Z, 0-9, _) -
\s
matches any whitespace character (space, tab, newline). -
\D
,\W
,\S
match the complements of\d
,\w
and\s
respectively (non-digits, non-alphanumerics, non-whitespace) -
.*
is greedy (matches as much text as possible);.*?
is non-greedy (matches as little text as possible). -
[0-9a-z]
matches any lowercase letter or digit. - To make a regex case-insensitive, use the
re.IGNORECASE
(orre.I
) flag. -
.
normally matches any character except a newline.re.DOTALL
(orre.S
) makes.
match any character including a newline. -
numRegex = re.compile(r'\d+')
: Thesub()
method with this regex can replace all sequences of digits with 'X'. -
re.VERBOSE
allows for more readable regexes by ignoring whitespace and adding comments. - Regex for numbers with commas every three digits:
r'\d{1,3}(,\d{3})*
- Regex for names with a capitalized first name followed by last name "Watanabe":
r'[A-Z][a-z]+\sWatanabe'
- Regex for sentences matching specific conditions with case-insensitive:
r'(Alice|Bob|Carol)\s(eats|pets|throws)\s(apples|cats|baseballs)\.'
, case-insensitive.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
This quiz explores the functions and methods of regular expressions in Python, including the use of re.compile()
, search()
, and findall()
. Test your understanding of regex patterns, raw strings, and capturing groups with practical examples. Perfect for anyone looking to enhance their Python programming skills!