Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua string.match to extract some values of a HTML

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)

like image 689
briba Avatar asked Sep 02 '13 00:09

briba


People also ask

How do I use string match in Lua?

> = string. match("foo 123 bar", '%d%d%d') -- %d matches a digit 123 > = string. match("text with an Uppercase letter", '%u') -- %u matches an uppercase letter U. Making the letter after the % uppercase inverts the class, so %D will match all non-digit characters.

How do I get substring in Lua?

sub() function takes three arguments in general, the first argument being the name of the string from which we want to extract a piece, the second argument is the i-th index or say, the starting index of the string piece that we want, and the third and the last argument is the j-th index of the last index of the string ...

Does Lua have regex?

Unlike several other scripting languages, Lua does not use POSIX regular expressions (regexp) for pattern matching. The main reason for this is size: A typical implementation of POSIX regexp takes more than 4,000 lines of code.

What is Lua Gmatch?

Lua Pattern matching The `gmatch` functiongmatch function will take an input string and a pattern. This pattern describes on what to actually get back. This function will return a function which is actually an iterator. The result of this iterator will match to the pattern.


1 Answers

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

like image 195
Nicol Bolas Avatar answered Sep 17 '22 17:09

Nicol Bolas