Problems with string:match()

Why does this work:

local str =  "Apple13"
print(str:match("Apple1"))

Output data: “Apple1”

But this doesn’t

local str =  "Apple-1-3"
print(str:match("Apple-1"))

Output data: nil

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

https://www.lua.org/manual/5.1/manual.html#5.4.1

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

2 Likes

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.