1

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?

2 Answers 2

3

You need to escape + in regular expressions to get a literal +, because it usually means "one or more instances of something":

regex = r'(\+|-)?\d*\.?\d*'  

And r makes it a "raw" string. Without the r, the regular expression escape sequences will be interpreted as string escape sequences, and they'll cause all sorts of problems. (\b being a backspace instead of a word boundary, and that kind of thing.)

Sign up to request clarification or add additional context in comments.

Comments

3

+ is a special character. You can use brackets to specify a range of characters, which is better than using an "or" with the pipe character in this case.:

regex = r'([+-])?\d*\.?\d*'  

Otherwise, you just need to escape it in your original version:

regex = r'(\+|-)?\d*\.?\d*'  

Using the r is the preferred way of specifying a regex string in python because it indicates a raw string, which should not be interpreted and reduces the amount of escaping you must perform with backslashes. It is just a python regex idiom you will see everywhere.

r'(\+|-)?\d*\.?\d*'
#'(\\+|-)?\\d*\\.?\\d*'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.