Using * in sets in python regex

In python 3.2.2, I came across strange errors when I try to use * in regex patterns. When * stads after / everything is fine. But when I try to remove / from patter, this code crashes with an error: sre_constants.error: invalid character range

import re foo = re.search("[^123+-/*]", "123+-/*w") if foo: print("foo") else: print("doo") 

In python docs, I found that using * is valid without any backslashes or other stuff. However, the problem with such code remains.

+4
source share
2 answers

Your problem is not * , it's a hyphen, which represents a range in a character class, in this case all characters are between + and / ( +,-./ ). Invalid range occurs because * precedes / .

If you want to include a literal hyphen in a character class, you need to either escape from it, put it at the end, or start:

 [^123+/*-] 
+5
source

Minus causes the last characters to be interpreted as a range of characters. [+-/] acutally means "any of +,-./ " (see ASCII table). When you replace / with * , you create an invalid range [+-*] because the ASCII code of the asterisk, 42, is less than the ASCII code for the plus, 43.

The solution just eludes the minus (then this is no longer a range).

+2
source

Source: https://habr.com/ru/post/1415766/


All Articles