2021-04-28 06:22:26 +02:00
|
|
|
|
2021-04-29 00:18:30 +02:00
|
|
|
-- So that in addition to regex.gsub(pattern, string), we can also do
|
|
|
|
-- pattern:gsub(string).
|
2021-04-28 06:22:26 +02:00
|
|
|
regex.__index = function(table, key) return regex[key]; end
|
|
|
|
|
|
|
|
regex.match = function(pattern_string, string)
|
2021-04-29 00:18:30 +02:00
|
|
|
local pattern = type(pattern_string) == "userdata" and
|
|
|
|
pattern_string or regex.compile(pattern_string)
|
2021-04-28 06:22:26 +02:00
|
|
|
return regex.cmatch(pattern, string)
|
|
|
|
end
|
|
|
|
|
2021-04-29 00:18:30 +02:00
|
|
|
-- Build off matching. For now, only support basic replacements, but capture
|
|
|
|
-- groupings should be doable. We can even have custom group replacements and
|
|
|
|
-- transformations and stuff in lua.
|
2021-04-28 06:22:26 +02:00
|
|
|
regex.gsub = function(pattern_string, string, replacement)
|
2021-04-29 00:18:30 +02:00
|
|
|
local pattern = type(pattern_string) == "userdata" and
|
|
|
|
pattern_string or regex.compile(pattern_string)
|
2021-04-28 06:22:26 +02:00
|
|
|
local offset, result, str, indices = 0, "", string
|
|
|
|
repeat
|
|
|
|
str = str:sub(offset)
|
|
|
|
indices = { regex.cmatch(pattern, str) }
|
|
|
|
if #indices > 0 then
|
|
|
|
result = result .. str:sub(offset, indices[1] - 1) .. replacement
|
|
|
|
offset = indices[2]
|
|
|
|
end
|
|
|
|
until #indices == 0 or indices[1] == indices[2]
|
|
|
|
return result .. str:sub(offset - 1)
|
|
|
|
end
|
|
|
|
|