Problems with string:match()

Why does this work:

local str =  "Apple13"

Output data: “Apple1”

But this doesn’t

local str =  "Apple-1-3"

Output data: nil

Because ‘-’ is a special character and must be escaped with ‘%’ if you want to match the actual character.

To match a ‘-’, try print( ("Apple-1-3"):match("Apple%-1") )


It still doens’t work. It gives me an error.

Nevermind. I had to put the % in front of the “-”

1 Like

whoops you’re right, I’ve edited my previous comment

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.