1.2.3AAABB,AAB,AA,AAABBB should give qw(1.2.3 AAA BB AA B AA AAA BBB) AABB,AAAB,ABBB should give qw(AA BB AAA B A BBB) #### use strict; sub chip{ local($_)=@_; if(/^(\d+ (?: \.\d+)* )? (.+) /x){ my($front) = $1; if(my(@rest) = $2 =~ /(A+) (B*) ,? /gx){ unshift(@rest, $front); grep {$_} @rest}}}