Add real regex support to lua

This commit is contained in:
2025-08-21 22:27:37 +02:00
parent e0d3b938e3
commit 6f2e76221a

View File

@@ -247,6 +247,7 @@ modified = false
initLuaHelpersLogger.Debug("Setting up Lua print function to Go")
L.SetGlobal("print", L.NewFunction(printToGo))
L.SetGlobal("fetch", L.NewFunction(fetch))
L.SetGlobal("re", L.NewFunction(evalRegex))
initLuaHelpersLogger.Debug("Lua print and fetch functions bound to Go")
return nil
}
@@ -481,3 +482,23 @@ func fetch(L *lua.LState) int {
fetchLogger.Debug("Pushed response table to Lua stack")
return 1
}
func evalRegex(L *lua.LState) int {
evalRegexLogger := processorLogger.WithPrefix("evalRegex")
evalRegexLogger.Debug("Lua evalRegex function called")
pattern := L.ToString(1)
input := L.ToString(2)
re := regexp.MustCompile(pattern)
matches := re.FindStringSubmatch(input)
matchesTable := L.NewTable()
for i, match := range matches {
matchesTable.RawSetString(fmt.Sprintf("%d", i), lua.LString(match))
}
L.Push(matchesTable)
evalRegexLogger.Debug("Pushed matches table to Lua stack")
return 0
}