Jump to content

Balanced brackets: Difference between revisions

→‎{{header|Perl}}: also Regexp::Common::balanced
(→‎{{header|Perl}}: also Regexp::Common::balanced)
Line 2,791:
<lang Perl>sub balanced {
shift =~ /^ ( [^\[\]]++ | \[ (?1)* \] )* $/x;
}</lang>
 
<code>Regexp::Common::balanced</code> can give such a regexp too (non-bracket chars allowed). Its recent versions use the subpattern recursion and are hence also only for Perl 5.10 and up.
 
<lang Perl>use Regexp::Common 'balanced';
my $re = qr/^$RE{balanced}{-parens=>'[]'}$/;
sub balanced {
return shift =~ $re;
}</lang>
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.