匹配的文本没有括号括起来

Toh*_*iko 3 perl parentheses

我还在学习Perl,如果这是一个显而易见的问题,请道歉.有没有办法匹配括号内未包含的文本?例如,搜索foo仅匹配第二行.

(bar foo bar)
bar foo (
bar foo 
   (bar) (foo)
)
Run Code Online (Sandbox Code Playgroud)

ike*_*ami 5

正则表达式模式具有隐式前导\G(?s:.)*?("跳过字符直到找到匹配").以下内容扩展了该定义,以将嵌套的parens视为要跳过的字符.

while (
   $string =~ m{
      \G (?&MEGA_DOT)*?

      ( foo )

      (?(DEFINE)
         (?<MEGA_DOT> [^()] | \( (?&MEGA_DOT)*+ \) )
      )
   }xg
) {
   say "Found a match at pos $-[1].";
}
Run Code Online (Sandbox Code Playgroud)