In python 3.2.2 I confronted with strange errors when I try to use * in regex patterns. When * stads after / all is ok. But when I try to remove / from patter, this code falls down with error: sre_constants.error: bad character range
import re
foo = re.search("[^123+-/*]", "123+-/*w")
if foo:
print("foo")
else:
print("doo")
In the python docs I found that using * is acceptable without any backslashes or other stuff. However problem with code like this remains.
Your problem isn’t
*, it’s the hyphen-minus which represents a range in a character class, in this case all characters between+and/(+,-./). The invalid range occurs because*comes before/.If you want to include a literal hyphen in a character class you have to either escape it or put it at the very end or start: