$test = "abc"; while $test =~ m/(a(b?c)?)/g print "$1\n";