Here's a quote from Gone with the Wind 👫
quote = "Frankly my dear, I don't give a darn."
Check if it contains
- a lowercase letter
- followed by a single space
- followed by the letter g
Expected result
Should return True
.
quote = "Frankly my dear, I don'{==t g==}ive a darn."
Regex Functions
Regex Patterns
Solution¶
import re
bool(re.search(pattern='[a-z]\sg', string=quote))
# True
Explanation
a-z
matches any lowercase character[a-z]\s
matches any lowercase character, followed by a whitespace character[a-z]\sg
matches any lowercase character, followed by a whitespace character, followed by the letter g
[re.search(pattern, string, ...)
][re-search] scans the string looking for the first location where the regular expression pattern produces a match. If it finds a match, it returns a match object, otherwise it returns None
.
re.search(pattern='[a-z]\sg', string=quote)
# <re.Match object; span=(23, 26), match='t g'>
Casting the result of re.search()
to a boolean indicates whether a match was found.
bool(re.search(pattern='[a-z]\sg', string=quote))
# True