simple regular expression module for lua.
luarocks install regex
creates a new regex object.
Parameters
pattern:string
: string containing expression to be compiled.flgs:string
: regular expression flags that can be combined with any of the following characters.i
: Do caseless matching.s
:.
matches anything including NL.m
:^
and$
match newlines within data.u
: Treat pattern and subjects as UTF strings.U
: Do not check the pattern forUTF
valid.x
: Ignore white space and#
comments.o
: compile-once mode that caching a compiled regex.g
: global match.j
: enable JIT compilation.
Returns
re:table
: regex object.err:string
: error message.
Example
local regex = require('regex')
local re, err = regex.new('a(b+)(c+)', 'i')
if re then
local arr, err = re:match('ABBBCCC')
if arr then
print(arr[1]) -- 'ABBBCCC'
print(arr[2]) -- 'BBB'
print(arr[3]) -- 'CCC'
else
print(err)
end
else
print(err)
end
matches a compiled regular expression against a given subject string. It returns matched substrings.
Parameters
sbj:string
: the subject string.offset:number
: offset in the subject at which to start matching.
Returns
arr:table
: array of matched substrings.err:string
: error message.
almost same as match
method but it returns all matched substrings except capture strings.
Parameters
sbj:string
: the subject string.offset:number
: offset in the subject at which to start matching.
Returns
arr:table
: array of matched substrings.err:string
: error message.
almost same as match
method but it returns offsets of matched substrings.
Parameters
sbj:string
: the subject string.offset:number
: offset in the subject at which to start matching.
Returns
arr:table
: array of offsets of matched substrings. 1st index is the start offset of matched substring, and 2nd index is the end offset of matched substring, and 3rd index is the start offset of 1st capture string, and 4th index is the end offset of 1st capture string, and so on.err:string
: error message.
almost same as match
method but it returns all offsets of matched substrings except capture strings.
Parameters
sbj:string
: the subject string.offset:number
: offset in the subject at which to start matching.
Returns
arr:table
: array of offsets of matched substrings. 1st index is the start offset of matched substring, and 2nd index is the end offset of matched substring, and so on.err:string
: error message.
returns true if there is a matched.
Parameters
sbj:string
: the subject string.offset:number
: offset in the subject at which to start matching.
Returns
ok:boolean
: true on matched.err:string
: error message.
same as the following code:
local re, err = regex.new( pattern, flgs )
if re then
return re:match( sbj, offset )
end
return nil, err
same as the following code:
local re, err = regex.new( pattern, flgs )
if re then
return re:matches( sbj, offset )
end
return nil, err
same as the following code:
local re, err = regex.new( pattern, flgs )
if re then
return re:indexof( sbj, offset )
end
return nil, err
same as the following code:
local re, err = regex.new( pattern, flgs )
if re then
return re:indexesof( sbj, offset )
end
return nil, err
same as the following code:
local re, err = regex.new( pattern, flgs )
if re then
return re:test( sbj, offset )
end
return nil, err