How to access the capture group in a regex subroutine?

前端 未结 1 1065
走了就别回头了
走了就别回头了 2021-01-19 15:49

I have a pattern that I want to use in a couple of places, but I want access to an inner part of the pattern. Is there a way to do this?

In this simplified

相关标签:
1条回答
  • 2021-01-19 16:37

    It is not possible, as the capture groups inside a DEFINE block are only "visible" inside that block and cannot be accessed from the pattern.

    See this perlre reference:

    This allows one to define subpatterns which will be executed only by the recursion mechanism... Note that capture groups matched inside of recursion are not accessible after the recursion returns, so the extra layer of capturing groups is necessary. ... Finally, keep in mind that subpatterns created inside a DEFINE block count towards the absolute and relative number of captures.

    So, even though you have 3 named capture groups, you can only access test group that was defined in the main pattern.

    0 讨论(0)
提交回复
热议问题