my $equation = '979x + 87y - 8723z = 274320'; # catch anything that proceeds an x, y or z, and then # catch that letter as well into another capture group my @parts = ($equation =~ /(.*?)([xyz])/ig); print "$_\n" for @parts;