Balanced brackets: Difference between revisions

Content added Content deleted
(→‎{{header|Perl}}: also Regexp::Common::balanced)
Line 2,791: Line 2,791:
<lang Perl>sub balanced {
<lang Perl>sub balanced {
shift =~ /^ ( [^\[\]]++ | \[ (?1)* \] )* $/x;
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>
}</lang>