Question

I'm using Lua string.match to extract some values of a HTML but I'm having some problems with some attributes.

To extract a phone number like this: 0000-0000, I'm using the mask:

local value = string.match(STRING, "%d%d%d%d-%d%d%d%d")

But Lua is returning something like this: "0000000"

Where is the "-" in the middle of the mask string?

And is there any way to do something like this:

"%d[4]-%d[4]" (specifying how many chars will appear in string)

Was it helpful?

Solution

- is a special control character in Lua patterns. Since you want the literal - character, you need to escape it with the % character. So use %-.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top