我必须在这些子程序调用中使用/ g标志吗?

问题描述 投票:0回答:1
^Name:\ (.*)\r?\n
Born:\ (?'date'(?:3[01]|[12][0-9]|[1-9])
            -(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)
            -(?:19|20)[0-9][0-9])\r?\n
Admitted:\ \g'date'\r?\n
Released:\ \g'date'$

这个例子来自这里:https://www.regular-expressions.info/subroutine.html(那里的第三部分的底部)

我知道/g是“全球”的旗帜,但网上没有关于它的好消息。这个例子中是否有必要,或者在引用命名捕获组时是否总是必须使用它?我可以在这里省略它,它会以同样的方式工作吗?我只是不明白它是如何工作的。

regex recursion subroutine
1个回答
0
投票

\g构造是Oniguruma(Pcre)函数调用。 而且,正如该链接所示,它与任何现代引擎函数调用相同。

注意,如果在同一个函数内调用相同的函数 它被称为递归。哪个与任何语言相同。

在您的示例中,它只是重用现有表达式 在date组(这里是第2组)。 所以,它只是一个函数调用。

Formatted:

 ^ 
 Name:\ 
 ( .* )                        # (1)
 \r? \n 
 Born:\ 
 (?'date'                      # (2 start)
      (?: 3 [01] | [12] [0-9] | [1-9] )
      -
      (?:
           Jan
        |  Feb
        |  Mar
        |  Apr
        |  May
        |  Jun
        |  Jul
        |  Aug
        |  Sep
        |  Oct
        |  Nov
        |  Dec
      )
      -
      (?: 19 | 20 )
      [0-9] [0-9] 
 )                             # (2 end)
 \r? \n 
 Admitted:\  \g'date' \r? \n 
 Released:\  \g'date' 
 $ 

指标:

----------------------------------
 * Format Metrics
----------------------------------
Cluster Groups               =   3

Capture Groups               =   2
       Named                 =   1

Recursions                   =   2
       \g Oniguruma (Pcre)   =   2

Character Classes            =   6
© www.soinside.com 2019 - 2024. All rights reserved.