Lua string.match to extract some values of a HTML
Asked Answered
I

1

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)

Inhospitable answered 2/9, 2013 at 0:23 Comment(2)
To help make the context clear can you also add what STRING contains when string.match is called?Corregidor
Actually string is a HTML after a GET method. :)Inhospitable
G
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 %-.

Granados answered 2/9, 2013 at 0:33 Comment(2)
@hjpotter92: I've been using Regex's for a while, so I forgot Lua's escape character.Granados
Didn´t work with \- but it works with %-. Thank you very much Nicol and hjpotter!Inhospitable

© 2022 - 2024 — McMap. All rights reserved.