#!/usr/bin/perl use strict; use diagnostics; use Data::Dumper; use Regexp::Common qw(balanced); my $re=$RE{balanced}{-parens=>'[]'}; # a valid string is: beginning, one or more [things] perhaps seperated by whitespace, end foreach ( 'a [foo] [bar][quux] grmbl42', #1 ok 'a [foo] [bar]bla[quux] grmbl42', #2 nok: has random crap instead of whitespace 'a [foo] [bar] grmbl42', #3 ok 'a [foo] grmbl42', #4 ok 'a [fo grmbl42', #5 nok: unbalanced 'a [fo]o] grmbl42', #6 nok: unbalanced 'a [fo[o] grmbl42', #7 nok: unbalanced 'a o] grmbl42', #8 nok: unbalanced 'a grmbl42', #9 nok: no [] at all ) { my $didit=/^ a # literal a \s* # optional whitespace (?: $re \s* )+ .* # something 42 # literal 42 $/x; print Dumper $_, $didit; };