sre_constants.error: bad character range
occurs because [\w- ]
is interpreted as a range. It's possible to use [\w -]
, but generally -
should be escaped inside character classes: [\w\- ]
.
Also, your expressions are not equivalent (aside from grouping). I'm not sure whether that was intentional, but note that the non-capturing version of (regex)?
is (?:regex)?
, not (?:regex)
. In order to behave akin to the first expression, the second one should be:
r'(?:\(([A-Z]{3})\))?\s*([\w\- ]+)?\s*(?:\(([0-9]{3})\))?'