确定正则表达式中存在哪些命名捕获?

发布于 2024-12-10 11:46:01 字数 122 浏览 2 评论 0原文

给定一个带有命名捕获的正则表达式,是否可以确定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吃颗糖壮壮胆 2024-12-17 11:46:01

在一般情况下这是不可能的,因为有人可能会插入 (??{....}) 代码,该代码插入会变成在比赛执行期间使用对迄今未见过的名称进行命名捕获的内容。

在特定情况下,您也许能够从 C API 深入了解底层结构。 Devel::Peek 建议您可能想要探索的一些地方:

% perl -MDevel::Peek -le 'Dump qr/(?<foo>\w+) (?&foo)/'
SV = IV(0x8033ac) at 0x8033b0
  REFCNT = 1
  FLAGS = (TEMP,ROK)
  RV = 0x803d40
  SV = REGEXP(0x8173d8) at 0x803d40
    REFCNT = 1
    FLAGS = (OBJECT,POK,FAKE,pPOK)
    PV = 0x216fc0 "(?^:(?<foo>\\w+) (?&foo))"
    CUR = 24
    LEN = 0
    STASH = 0x803b50    "Regexp"
    EXTFLAGS = 0x600000 (USE_INTUIT_NOML,USE_INTUIT_ML)
    INTFLAGS = 0x1
    NPARENS = 1
    LASTPAREN = 0
    LASTCLOSEPAREN = 0
    MINLEN = 3
    MINLENRET = 3
    GOFS = 0
    PRE_PREFIX = 4
    SEEN_EVALS = 0
    SUBLEN = 0
    SUBBEG = 0x0
    ENGINE = 0x162b60
    MOTHER_RE = 0x80fab0
    PAREN_NAMES = 0x80fa40
    SUBSTRS = 0x2044b0
    PPRIVATE = 0x206360
    OFFS = 0x204280

例如,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:

% perl -MDevel::Peek -le 'Dump qr/(?<foo>\w+) (?&foo)/'
SV = IV(0x8033ac) at 0x8033b0
  REFCNT = 1
  FLAGS = (TEMP,ROK)
  RV = 0x803d40
  SV = REGEXP(0x8173d8) at 0x803d40
    REFCNT = 1
    FLAGS = (OBJECT,POK,FAKE,pPOK)
    PV = 0x216fc0 "(?^:(?<foo>\\w+) (?&foo))"
    CUR = 24
    LEN = 0
    STASH = 0x803b50    "Regexp"
    EXTFLAGS = 0x600000 (USE_INTUIT_NOML,USE_INTUIT_ML)
    INTFLAGS = 0x1
    NPARENS = 1
    LASTPAREN = 0
    LASTCLOSEPAREN = 0
    MINLEN = 3
    MINLENRET = 3
    GOFS = 0
    PRE_PREFIX = 4
    SEEN_EVALS = 0
    SUBLEN = 0
    SUBBEG = 0x0
    ENGINE = 0x162b60
    MOTHER_RE = 0x80fab0
    PAREN_NAMES = 0x80fa40
    SUBSTRS = 0x2044b0
    PPRIVATE = 0x206360
    OFFS = 0x204280

For example, PAREN_NAMES looks promising.

But that seems like a lot of work. What do you want to do this for?

梦与时光遇 2024-12-17 11:46:01

您可以查看 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文