what's the regular expression to find two sets of parenthesis in a row using perl?

前端 未结 3 1189
我在风中等你
我在风中等你 2021-01-23 09:26

I have rows with different sets of parenthesis. For example

     (sdfsfs) (sfdsfd) 
     (sdfsfs) (sfdsfd) (sfdsfd) 
     (sdfsfs) (sfdsfd) (sfdsfd) (sfdsfd)
            


        
3条回答
  •  时光说笑
    2021-01-23 09:46

    This is a better solution.

    while ( $text =~ //(?m)^(?=(?:.*?(?&parens)){2})(?!(?:.*?(?&parens)){3}).+$(?(DEFINE)(?\([^)\n]*\)))/g )
    {
        print $&,"\n";
    }
    

    Formatted:

     (?m)                          # Multi-line mode
     ^                             # BOL
     (?=                           # Must be 2 paren's blocks
          (?:
               .*? (?&parens) 
          ){2}
     )
     (?!                           # Cannot be 3 paren's blocks
          (?:
               .*? (?&parens) 
          ){3}
     )
     .+                            # Get the entire line
     $                             # EOL
    
     (?(DEFINE)
          (? \( [^)\n]* \) )    # (1)
     )
    

提交回复
热议问题