Regular expressions

This page documents the regex module.

General concepts

TODO...

Application Programming Interface


new(pattern)

Create and return a new regular expression (regex) from a string pattern. The regex can be matched against any string.

local re = regex.new("^(..)")
-- Do something with re...

See also: pattern


match(re, subject)

Match regular expression re against string subject. Returns true if there was a match, false otherwise.

See also: count, capture, has_match


has_match(re)

Returns true if the last call to match was sucessful, and false if was unsuccessful or if match was not called.

See also: match


capture(re, nth)

Returns the nth captured sub-expression in the last successful call to match. If nth equals 0, the whole matched string is returned, even if no sub-expression was captured.

Note: This function returns an empty string if nth is greater than the number returned by the count function.

See also: count, match, first, last


count(re)

Returns the number of captured sub-expressions in the last call to match. This function returns 0 if there was no captured sub-expression, if there was no match or if match was not called.

local re = regex.new("^a(...)(..)(..)")

-- Print "bra", "ca", "da"
if regex.match(re, "abracadabra") then
    for i=1, regex.count(re) do
        local text = regex.capture(re, i)
        print(text)
    end       
end

See also: capture, match


pattern(re)

Returns the pattern (as a string) from which the re regular expression was constructed.

See also: new


first(re, nth)

Returns the index of the first character of the nth capture. If nth equals 0, it returns the index of the first character in the whole matched string.

See also: capture, last


last(re, nth)

Returns the index of the last character of the nth capture. If nth equals 0, it returns the index of the last character in the whole matched string.

See also: capture, first