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)
> = 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.
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 ...
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.
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.
-
is a special control character in Lua patterns. Since you want the literal -
character, you need to escape it with the %
character. So use %-
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With