.* means “zero or more of any character (other than a newline), as
many as you can and still have the rest of the pattern match”.
So first gsub tries to find this, and finds:
“trying to match whole string”
so it replaces it:
“MATCH:trying to match whole string”
Now, the ‘g’ in ‘gsub’ means ‘global’, so it doesn’t stop after just
one match/replacement. It keeps on going through the string, finding
more things to replace. At this point, it starts looking for the
pattern again, this time starting after the ‘g’ in ‘string’. Can it
find “zero or more of any character (other than a newline)”? Yes, it
can. It finds zero of them. So then it replaces that (empty string)
with “MATCH:”, resulting in:
“MATCH:trying to match whole stringMATCH:”