use warnings; use strict; my $str = 'Iteration {Applied Field} {Total Energy} Mx'; while(length($str)>0){ #print "str:$str:\n"; if($str =~ m/^({([\s\w:]+)}(\s+)?)/){ print "1: $2\n"; $str =~ s/^$1//; } elsif($str =~ m/^(([\w:]+)(\s+)?)/){ print "2: $1\n"; $str =~ s/^$1//; } else{die "error";} } Result: 2: Iteration 1: Applied Field 1: Total Energy 2: Mx