我需要帮助解决一些 Python 正则表达式
我试图正确地理解下面的内容,但我的推理仍然有很大的漏洞。什么是?::,有人可以为我正确解释一下吗
rule_syntax = re.compile('(\\\\*)'\
'(?:(?::([a-zA-Z_][a-zA-Z_0-9]*)?()(?:#(.*?)#)?)'\
'|(?:<([a-zA-Z_][a-zA-Z_0-9]*)?(?::([a-zA-Z_]*)'\
'(?::((?:\\\\.|[^\\\\>]+)+)?)?)?>))')
I have tried to properly wrap my head around the below but I still have big hole in my reasoning. What is ?::, and could someone explain it properly for me
rule_syntax = re.compile('(\\\\*)'\
'(?:(?::([a-zA-Z_][a-zA-Z_0-9]*)?()(?:#(.*?)#)?)'\
'|(?:<([a-zA-Z_][a-zA-Z_0-9]*)?(?::([a-zA-Z_]*)'\
'(?::((?:\\\\.|[^\\\\>]+)+)?)?)?>))')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能希望研究两个工具来帮助您理解
There are two tools that you may wish to look into to help with your understanding
(?:expr)
就像普通的括号(expr)
一样,只不过是为了稍后检索组(反向引用、re.sub
、或MatchObject.group
),以?:
开头的括号组被排除。如果您需要捕获括号中的复杂表达式以对其应用另一个运算符(如*
),但又不想将其与您实际需要检索的组混合在一起,这可能会很有用之后。'?::
只是?:
后跟文字:
。(?:expr)
is just like normal parentheses(expr)
, except that for purposes of retrieving groups later (backreferences,re.sub
, orMatchObject.group
), parenthesized groups beginning with?:
are excluded. This can be useful if you need to capture a complex expression in parentheses to apply another operator like*
to it, but don't want to get it mixed in with groups that you'll actually need to retrieve later.'?::
is simply?:
followed by a literal:
.