#!/usr/bin/perl my $q; $q = qr/(?:\[(?:(?>[^\[\]]+)|(??{$q}))*\])/; while ( defined( $_ = ) ) { while (/(\w{1,2})($q)/g) { print $1, $2, $/; #push(@{$hash{$1}},$2); } } __DATA__ T[[pP]attern] X[] X[xx[xxx[xx]]]