#!/usr/bin/perl use strict; use warnings; my $string = qq~['S', ['NP', ['XP', ['YP', ['ZP'], ['ZP']]] 'VP']]~; our $pat; $pat = qr!\[(?>(?>[^\[\]]+)|(??{$pat}))*\]!; if( $string =~ /^($pat)$/ ){ print "matcht $1!\n"; }