$cmd=q|&COMPAREEQUAL(First-param.one, Second.param,Third-param)|; my $re = qr{ \G (?: \& | (\w+) # $1 \( | (?: ( # $2 [^,\)\s]+ ) | (?: [,\s]+ ) ) | \) ) }mx; while( $cmd =~ m{$re}g ){ use Data::Dump; dd [ $1, $2 ]; } __END__ [undef, undef] ["COMPAREEQUAL", undef] [undef, "First-param.one"] [undef, undef] [undef, "Second.param"] [undef, undef] [undef, "Third-param"] [undef, undef]