exclusion in character classes | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

exclusion in character classes

Why doesn't ^ in the beginning of pattern = r"[^A-Z]" exclude the uppercase letters in re.search(pattern, "AbCdEfG123")?

14th Jul 2020, 3:02 AM
Rana Ahmadimehr
Rana Ahmadimehr - avatar
4 Answers
+ 3
Can you please display the link of your code?
14th Jul 2020, 3:05 AM
James Clark I. Vinarao
James Clark I. Vinarao - avatar
+ 2
try the carat on the outside of the square bracket
14th Jul 2020, 3:32 AM
Slick
Slick - avatar
+ 2
my mistake youre pattern works. https://code.sololearn.com/c7hye0QRvTln/?ref=app
14th Jul 2020, 3:41 AM
Slick
Slick - avatar
0
here is the code: import re pattern = r"[^A-Z]" if re.search(pattern, "this is all quiet"): print("Match 1") if re.search(pattern, "AbCdEfG123"): print("Match 2") if re.search(pattern, "THISISALLSHOUTING"): print("Match 3") Result: Match 1 Match 2
14th Jul 2020, 3:34 AM
Rana Ahmadimehr
Rana Ahmadimehr - avatar