在正则表达式匹配中忽略括号中的部分

时间:2016-08-11 18:46:07

标签: regex perl

现在我有这个正则表达式:

/regular(\[[a-z]*\])* expression/i

正确匹配此字符串:

Regular[AECC][XVK] Expression

但未能与这些匹配:

R[AECC]egular [XVK]Expression
R[ABC]egular Expression
Regular[xx] Expressio[]n

如何将以上所有与单个正则表达式匹配?

1 个答案:

答案 0 :(得分:3)

/
   r (?:\[[a-z]*\])?
   e (?:\[[a-z]*\])?
   g (?:\[[a-z]*\])?
   ...
   i (?:\[[a-z]*\])?
   o (?:\[[a-z]*\])?
   n
/ix

my $pat = join '(?:\\[[a-z]*\\])?', map quotemeta, split //, 'regular expression';
my $re = qr/$pat/i;
/$re/

s/\[[a-z]*\]//ig;
/regular expression/i