diff --git a/processor/processor.go b/processor/processor.go index f85eb97..3b7ab73 100644 --- a/processor/processor.go +++ b/processor/processor.go @@ -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 +}