phpregex来查找嵌套的多行cfml注释


php regex to find nested multiline cfml comments

使用php,我想要一个正则表达式,它与ColdFusion支持的样式的嵌套AND多行注释相匹配:

1. <!--- this is a single comment line --->
2. <!---
multiline
comment
--->
3. <!---
multiline <!--- nested --->
comment <!--- comment --->
--->

这三种情况都是单个有效的cfml注释。我发现许多正则表达式适用于前两种情况,但不适用于第三种情况,这才是真正的错误。感谢您的帮助。

您需要使用递归模式:

<!---(?>[^<-]+|-(?!-->)|<(?!!---)|(?R))*--->

详细信息:

<!---
(?>              # open an atomic group
    [^<-]+       #   all that is not a < or a -
  |              #  OR
    -(?!-->)     #   a - not followed by -->
  |              #  OR
    <(?!!---)    #   a < not followed by !---
  |              #  OR
    (?R)         #   recursion (repeat the whole pattern itself)
)*               # close the atomic group, repeat zero or more times
--->

您可以使用带有gs选项的递归PCRE正则表达式:

(?<comment><!---(?(?=<!---)'g<comment>|.)*?--->)

演示

故障(x模式):

(?<comment>         # define group "comment"
  <!---             # match a "<!---"
  (?(?=<!---)       # is the next sequence a "<!---"
    'g<comment>     #   yes: match a comment (recurse)
    |.              #   no: match a character
  )*?               # and repeat
  --->              # until a "--->"
)                   # close "comment" definition