Usage of ^ in character class | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Usage of ^ in character class

Why is it matching 1st one? The characters are small letters and r"[^A-Z]"--Does this search for only 1st pattern in the string or the whole string? 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")

7th Sep 2017, 12:38 AM
Krishna Vihari Pinnamaneni
Krishna Vihari Pinnamaneni - avatar
1 Answer
+ 1
When ^ is used as the first character inside a regex class it means that you are looking for any character except those within the class. Your regex matches to any character that is not a capitol letter A-Z. This is why the last search is the only one that doesn't return true. https://docs.python.org/3/howto/regex.html
7th Sep 2017, 1:36 AM
ChaoticDawg
ChaoticDawg - avatar