python – Regex consonant mismatch
I am trying to use Python and regex to search for words that look in certain defined ways. Can anyone tell me why the following code returns the word “choosy” as part of the output? I thought I made sure “oo” should be followed by any amount of consonants and then a word boundary, not “sy”.
Code:
import re
pattern = r'\b[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z]*oo([b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z]*\b)'
# Example list of words
words = ["choosy", "book", "boot", "booze", "loose", "look", "foot", "moo", "food", "moose"]
# Find matches
matches = [word for word in words if re.fullmatch(pattern, word)]
print("Matches:", matches)
Output:
Matches: ['choosy', 'book', 'boot', 'look', 'foot', 'moo', 'food']
Read more here: Source link