use strict; use warnings; use Text::Balanced 'extract_bracketed'; my $string = q/the users contain (bbc (333)) BLAH BLAH (ddc (223)) BLAH BLAH(ccc (123))/; while( length $string ) { my( $name, $id, $rest ) = extract_record($string); print "Name: $name, ID: $id\n"; $string = $rest; } sub extract_record { my $string = shift; my( $unwanted, $wanted, $rest ) = extract($string); return (extract($wanted))[0,1], $rest; } sub extract { my $input = shift; my( $balanced, $rest, $prefix ) = extract_bracketed($input, '()', qr/[^(]*/); $balanced =~ s/^\(|\)$//g if length $balanced; return map { trim($_) } $prefix, $balanced, $rest; } sub trim { my $string = shift; $string =~ s/^\s+|\s+$//g; return $string; } #### my $string = q/the users contain (bbc (333)) BLAH BLAH (ddc (223)) BLAH BLAH(ccc (123))/; my $re = qr/ \( (? [^(]+ ) # Capture name. \( (? [^)]+ ) # Capture ID. \)\) # Closing parens. /x; print "Name: $+{name}, ID: $+{id}\n" while $string =~ m/$re/g;