In addition to what everyone else has said, I would also like to note that you are using $line =~ s/...//, which is a substitution. That's not what you want. You want to use $line =~ m/.../g. The /g modifier is used for global matching, which means on each iteration of the loop, it will save the current position in the string and match from there on the next iteration.