Is there a way to check if a string matches exactly a regular expression in Python? For example, if the regular expression is \d\s\d , it should contain lines 1 5 , 8 2 , etc., but not lorem 9 4 ipsum or a7 3 .
\d\s\d
1 5
8 2
lorem 9 4 ipsum
a7 3
Strings and regex are different types. I think you want to check if the string is "exactly equal" to the regular expression, but the regular expression matches the whole line. To do this, simply use the beginning and end of the anchors ( ^ and $ , respectively) in the regular expression. For instance:
^
$
^\d\s\d$
instead