Balanced brackets: Difference between revisions

→‎{{header|Perl}}: add generalized regex version that can handle inputs with non-bracket characters
(→‎{{header|Perl}}: make the code do what was actually asked; make regex solution more idiomatic and put it first; remove use of deprecated 'when' keyword)
(→‎{{header|Perl}}: add generalized regex version that can handle inputs with non-bracket characters)
Line 2,757:
bad: ']][]]][[][][[][['
</pre>
 
If input strings are allowed to contain unrelated characters, this can be extended to:
 
<lang Perl>sub balanced {
shift =~ /^ ( [^\[\]]++ | \[ (?1)* \] )* $/x;
}</lang>
 
Alternative implementation, using straightforward depth counting:
Anonymous user