Why pattern = r"\b(cat)\b" should match with ">cat<"? Don't the boundaries should be the same symbol?can they be also brackets? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why pattern = r"\b(cat)\b" should match with ">cat<"? Don't the boundaries should be the same symbol?can they be also brackets?

I am trying to understand regular espressions in python with the module re

17th Sep 2016, 3:28 PM
Giovanni Gatto
Giovanni Gatto - avatar
2 Answers
+ 1
Brackets have a special meaning for regular expressions. To use them in your expression as regular brackets, add a backslash before them: \( \).
17th Sep 2016, 4:04 PM
Zen
Zen - avatar
+ 1
pattern = r"\b(cat)\b" where \b = the empty string (not words) between word characters. so > and < is not a word, therefore Python considers it an empty space. I hope this helps!
23rd Mar 2017, 10:14 PM
phil
phil - avatar