5

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)

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
briba
  • 2,857
  • 2
  • 31
  • 59

1 Answers1

7

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

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982