Updated April 19, 2023
Introduction to Lua RegEx
The Lua Regular Expression or RegEx is a sequence of characters which forms a search pattern and that is used to match a combinations of characters in a strings. The RegEx can be used to verify whether a string contain the specified search pattern or not. Unlike other languages the lua RegEx is different, it is more limited and has different syntax.
Lua RegEx Functions
The Lua programming offers a set of functions that allows us to search a string for a match as listed below:
- find(string, pattern [, init [, plain]]): This function returns to start and end index of a match pattern in a string.
- match(string, pattern [, index]): This function matches a pattern once the matching starts at the given index.
- gmatch(string, pattern): This function returns a function which iterates through all matches pattern in string.
- gsub(string, pattern, repl [ , n]): This function used to replace the matched string by substrings and n specify the n number of replacement.
Lua RegEx Metacharacters
The Lua programming offers a set of metacharacters, special sequence and sets which have a special meaning as listed below:
- . – : This is a metacharacter which matches all characters.
- %a: This is a special sequence which matches all letters.
- %l: This is a special sequence which matches all lowercase letters.
- %u: This is a special sequence which matches all uppercase letters.
- %d: This is a special sequence which matches all digits.
- %s: This is a special sequence which matches all whitespace characters.
- %x: This is a special sequence which matches all hexadecimal digits.
- %p: This is a special sequence which matches all punctuation characters.
- %g: This is a special sequence which matches all printable characters except space.
- %c: This is a special sequence which matches all control characters.
- [set]: This is a set which matches the class which is the union of all characters in set.
- [^set]: This is a special sequence which matches the complement of set.
- +: This is a greedy match which matches 1 or more occurrences of previous character class.
- *: This is a greedy match which matches 0 or more occurrences of previous character class.
- ?: This is a match exactly which matches 0 or 1 occurrence of previous character class.
- – -: This is a lazy match which matched 0 or more occurrences of previous character class.
Example of Lua RegEx
Example as below:
Code:
-- create string to match pattern
str = 'Apple'
print( "The string is : ", str)
-- 'pl' match in string
print( "string.find( str, 'pl') : ", string.find( str, 'pl'))
-- 'lua' will not be match in string
print( "string.find( str, 'lua') : ", string.find( str, 'lua'))
-- e.. match e and any two characters
print( "string.find( 'Hello', 'e..') : ", string.find( "Hello", 'e..'))
-- match 3 sequence of digit
print( "string.match(\"Hello, 123 \", '%d%d%d') : ", string.match("Hello, 123 ", '%d%d%d'))
--
print( "string.match('banana', '[na][an]') : ", string.match("banana", '[na][an]'))
-- you can specify a range of characters using -
print( "string.match('123', '[0-9]') : ", string.match("123", '[0-9]'))
-- Repetition examples
print( "string.match('Apples', 'Apples?') : ", string.match("Apples", 'Apples?'))
print( "string.match('Apple', 'Apples?') : ", string.match("Apple", 'Apples?'))
print( "string.match('Apple', 'Apples') : ", string.match("Apple", 'Apples'))
print( "string.match('abcd', 'a.*') : ", string.match("abc", 'a.*'))
-- $ matches the end of the string
print( "string.match('abcd', 'a.-$') : ", string.match("abcd", 'a.-$'))
-- .- part matches nothing
print( "string.match('abcd', 'a.-$') : ", string.match("abcd", 'a.-'))
-- ^ matches the start of the string
print( "string.match('abcd', '^.-b') : ", string.match("abcd", '^.-b'))
-- gsub() example
print( "string.gsub('Hello!, John', 'John', 'Johny') : ", string.gsub("Hello!, John", "John", "Johny"))
Output:
As in the above lua program we can see some of the examples of pattern matching with help of regular expression, where we can see the find(), match() and gsub() functions are used. The find() function used to check whether the pattern found or not, if pattern found, then return the start and end position of the patter found in string, if patter not found then return “nil” as output. The match function is used to match the pattern and return the matched group from the string as in above example string is “ Hello, 123 “, and pattern is “%d%d%d”(which match three continues digits), so here the matching string is “123” which we can see in the output as well. Next the gsub() function used where it match the pattern(second parameter) and the matched string replace by third string passed as here is “Johny”. The output of this method return the string (with matched replaced) and the number of replacement as in output it is 1 because only one occurrence matched.
Conclusion
The Lua RegEx is a sequence of characters which forms a search pattern and that is used to match a combinations of characters in a strings. The Lua programming provided some of the functions like match(), gmatch(), find(), gsub() and metacharacters to work easily and efficiently for the pattern matching as above we have seen the example.
Recommended Articles
We hope that this EDUCBA information on “Lua RegEx” was beneficial to you. You can view EDUCBA’s recommended articles for more information.