Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

LUtilsRegex reference

Lexize edited this page Sep 13, 2022 · 3 revisions

LUtilsRegex

LUtils submodule that provides functions to work with Regex in Figura.


isMatches(String pattern, String input): Boolean
Returns true if string matches specified pattern.

matches(String pattern, String input, [Integer flags]): LUtilsRegexMatch[]
Return table with all matches inside input.

replace(String pattern, String input, Object replaceValue, [Integer flags]): String
Return string replaced by pattern.
replaceValue may be replace string, or function that returns replacement value.
Function that provided in replaceValue accepts LUtilsRegexMatch as first argument.


How to use:

local regex = lutils.regex;
local pattern = "(.+)=(.+)";
local str = "foo=bar";

print(regex:isMatches(pattern, str)) -- true
print(regex:isMatches(pattern, "foo-bar")) -- false

local matches = regex:matches(pattern, str);
local match = matches[1];
local groups = match:groups();

for k,v in pairs(groups) do
    print(k..": "..v:content());
end
-- 1: foo
-- 2: bar
-- 0: foo=bar

local replace_string = regex:replace(pattern, str, "$2=$1");
print(replace_string) -- bar=foo
local replace_lambda = regex:replace(pattern, str, function (m)
    local g = m:groups();
    return g[1]:content()..": "..g[2]:content();
end);
print(replace_lambda) -- foo: bar