group name can't start with number?

我怕爱的太早我们不能终老 提交于 2021-02-17 02:56:47

问题


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


回答1:


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"



回答2:


If this is the pattern you are searching r'(?P<74xxx>[0-9]+)' and you wanted to include ? in your search pattern then you have to prepend \ with it since it is the special character in python. So your search pattern should be r'(\?P<74xxx>[0-9]+)'.



来源:https://stackoverflow.com/questions/39332611/group-name-cant-start-with-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!