perl -d DB<1> $equation = '979x + 87y - 8723z = 274320' DB<2> $vars='xyz' DB<3> $operators=qr/[-+*\/%=]/ DB<4> x $equation =~ /([^\s$vars]+|[$vars]|$operators)/g 0 979 1 'x' 2 '+' 3 87 4 'y' 5 '-' 6 8723 7 'z' 8 '=' 9 274320