I’m following a tutorial about regular expression. I’m getting an error when doing the following:
regex = r'(+|-)?\d*\.?\d*'
Apparently, Python doesn’t like (+|-). What could be the problem?
Also, what could be the issue with not adding r ahead of the regex?
You need to escape
+in regular expressions to get a literal+, because it usually means “one or more instances of something”:And
rmakes it a “raw” string. Without ther, the regular expression escape sequences will be interpreted as string escape sequences, and they’ll cause all sorts of problems. (\bbeing a backspace instead of a word boundary, and that kind of thing.)