Strange Patterns Output

When I input this string


print(string.gsub("jesus|christ|it's|a3monkeylol", "%w-", "<3Kogitsune"))

into the lua demo (found here: http://www.lua.org/cgi-bin/demo),

I get the strange output of:


<3Kogitsunej<3Kogitsunee<3Kogitsunes<3Kogitsuneu<3Kogitsunes<3Kogitsune|<3Kogitsunec<3Kogitsuneh<3Kogitsuner<3Kogitsunei<3Kogitsunes<3Kogitsunet<3Kogitsune|<3Kogitsunei<3Kogitsunet<3Kogitsune'<3Kogitsunes<3Kogitsune|<3Kogitsunea<3Kogitsune3<3Kogitsunem<3Kogitsuneo<3Kogitsunen<3Kogitsunek<3Kogitsunee<3Kogitsuney<3Kogitsunel<3Kogitsuneo<3Kogitsunel<3Kogitsune	30


Instead of replacing each letter with <3Kogitsune, it shoves it right before each letter. Why?

(Also, I used <3Kogitsune because he was just teaching me patterns and he couldn’t figure this out as well.)

I just tried that, it did what it was supposed to do.

To replace every single character with <3Kogitsune use this:


print(string.gsub("jesus|christ|it's|a3monkeylol", "[%w|']", "<3Kogitsune"))

I already know how to do that, I was just curious on why “%w-” does that instead of what I had expected it to do.

You didn’t say you used “%w-” in your first post. “%w-” matches 0 or more occurences of all alphanumeric characters, it always matches the shortest sequence, in your case the shortest sequence is 0 occurences of an alphanumeric character, so gsub will replace the gap between each character with “<3Kogitsune”.

My bad - I forgot to put it there.

Thanks.

Because ‘-’ matches the shortest possible match, including 0 matches. ‘%w’ is what you’re looking for if you want to replace every letter.

However, it’s 0 OR MORE, which should make it replace the gaps between the letters as well as the letters too?

0 or more, yes, but always the shortest.