确定正则表达式中存在哪些命名捕获?
给定一个带有命名捕获的正则表达式,是否可以确定 re 中存在哪些命名捕获?
注意:我只有正则表达式 - 我没有与 re 匹配的字符串。我想知道是否有一种方法可以查看 re 的结构并找到 re 中使用的所有命名捕获。
Given a regular expression with named captures, is it possible to determine which named captures are present in the re?
Note: I only have the regular expression - I don't have a string which matches the re. I want to know if there is a way to look into the structure of the re and find all of the named captures used in the re.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在一般情况下这是不可能的,因为有人可能会插入
(??{....})
代码,该代码插入会变成在比赛执行期间使用对迄今未见过的名称进行命名捕获的内容。在特定情况下,您也许能够从 C API 深入了解底层结构。
Devel::Peek
建议您可能想要探索的一些地方:例如,
PAREN_NAMES
看起来很有前途。但这似乎需要很多工作。你想这样做是为了什么?
That’s not possible in the general case, because someone could have a
(??{....})
code insert that turns into something that uses a named capture of a hitherto unseen name during match execution time.In a specific case you might be able to dig into the underlying structure from the C API.
Devel::Peek
suggests some places you might want to poke around:For example,
PAREN_NAMES
looks promising.But that seems like a lot of work. What do you want to do this for?
您可以查看
YAPE::Regex::Explain
。其他帖子似乎表明 http://rick.measham.id.au/paste/explain .pl 是它的一个前端,但可以肯定的是它的安装和使用并不难。我不确定它的命名捕获效果如何,当然,正如 tchrist 已经说过的那样,有些情况(可能)是不可能的。
You might look at
YAPE::Regex::Explain
. Other posts seem to suggest that http://rick.measham.id.au/paste/explain.pl is a frontend for it, but to be sure its not hard to install and use.I'm not sure how well it does named captures and certainly, as tchrist already said, there are cases which will be (probably) impossible.