warl0ck warl0ck - 2 months ago 9
Python Question

group name can't start with number?

It looks like I can't use regex like this one,

(?P<74xxx>[0-9]+)


With re package it would raise and error,

sre_constants.error: bad character in group name u'74xxx'


It looks like I can't use group names that starts with a number, why?

P.S golang does not have such problem, so does many other languages

Answer

Given the doc:

Group names must be valid Python identifiers

As the variables, identifiers mustn't start with a number in Python. See more about identifiers here:

identifier  ::=     (letter|"_") (letter | digit | "_")*
letter      ::=     lowercase | uppercase
lowercase   ::=     "a"..."z"
uppercase   ::=     "A"..."Z"
digit       ::=     "0"..."9"
Comments