A Bug? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

A Bug?

WHY the following only outputs Match 2 not Match 1? import re pattern = r"[aeiou][rgp]" if re.search(pattern, "grey"): print("Match 1") if re.search(pattern, "qwertyuiop"): print("Match 2") if re.search(pattern, "rhythm myths"): print("Match 3") # Output: # Match 2

31st Mar 2017, 9:12 PM
Ren Wang
Ren Wang - avatar
3 Answers
0
I figured out: The first [aeiou] is matched than the 2nd [rgp]. In the case of "grey", after 'e' is matched to the first [aeiou], the character left is 'y', which does not match any of the [rgp]. I think in the case "qwertyuiop", 'e' is matched first [aeiou], and then 'r' immediately to the second [rgp].
31st Mar 2017, 9:18 PM
Ren Wang
Ren Wang - avatar
0
pattern = r"[aeiou][rgp]" You are searching for a vowel letter ('a', 'e', 'i', 'o' or 'u') followed by either 'r' or 'g' or 'p'. And that is matched in "qwertyuiop" only (Match2), there is 'e' followed by 'r' and also 'o' followed by 'p'. However in Match1, there is 'e' followed by 'y' which doesn't match the pattern
31st Mar 2017, 9:19 PM
Moataz El-Ibiary
Moataz El-Ibiary - avatar
0
One or more repetitions of a non-vowel, a vowel and a non-vowel
30th Dec 2022, 7:52 AM
Aniket Pawar
Aniket Pawar - avatar