Question address: Sololearn > Python > reg ex > more metacharacters > Q 1 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Question address: Sololearn > Python > reg ex > more metacharacters > Q 1

The question says: What would [a^]* match? Correct option showed currently is ' Zero or more repetitions of "a" or "^" ' but that does not make any sense as: [a^]* here there's nothing after ^ this means anything would be a match and * denotes zero or multiple repetitions of the thing before. Even if my assumptions are wrong, still the code below runs successfully: import re pattern = r"[a^]*" if re.match(pattern, "egg"): print("Match confirmed") Run this code & it will easily contradict the correct option.

15th Feb 2019, 5:38 PM
Pranav Gadre
Pranav Gadre - avatar
2 Answers
+ 4
The pattern doesn't match 'egg', but an empty string with length 0. You can easily check that with print(re.match(pattern, 'egg')). It's an empty string because that's the only possible match (0 * a or ^ = empty string). Also, I think you might confuse [^] with ^. Inside square brackets, ^ doesn't have any special meaning and doesn't denote the end of the string. Long story short, the answer is correct (although a bit confusing) because it zero occurrences of a or ^, aka an empty string.
15th Feb 2019, 5:56 PM
Anna
Anna - avatar
+ 1
Thanks for answering & clarifying!
15th Feb 2019, 6:22 PM
Pranav Gadre
Pranav Gadre - avatar