Add real regex support to lua
This commit is contained in:
@@ -247,6 +247,7 @@ modified = false
|
|||||||
initLuaHelpersLogger.Debug("Setting up Lua print function to Go")
|
initLuaHelpersLogger.Debug("Setting up Lua print function to Go")
|
||||||
L.SetGlobal("print", L.NewFunction(printToGo))
|
L.SetGlobal("print", L.NewFunction(printToGo))
|
||||||
L.SetGlobal("fetch", L.NewFunction(fetch))
|
L.SetGlobal("fetch", L.NewFunction(fetch))
|
||||||
|
L.SetGlobal("re", L.NewFunction(evalRegex))
|
||||||
initLuaHelpersLogger.Debug("Lua print and fetch functions bound to Go")
|
initLuaHelpersLogger.Debug("Lua print and fetch functions bound to Go")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -481,3 +482,23 @@ func fetch(L *lua.LState) int {
|
|||||||
fetchLogger.Debug("Pushed response table to Lua stack")
|
fetchLogger.Debug("Pushed response table to Lua stack")
|
||||||
return 1
|
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
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user