Reputation: 185
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.
Upvotes: 4
Views: 2081
Reputation: 681
The minus is causing the last characters to get interpreted as a character range. [+-/]
acutally means "any of +,-./
" (see the ASCII table). When you replace the /
by *
, you're creating the invalid range [+-*]
because the ASCII code of the asterisk, 42, is less than the ASCII codes of the plus, 43.
The solution is simply escaping the minus (then it's not a range any more).
Upvotes: 2
Reputation: 354694
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:
[^123+/*-]
Upvotes: 5