Cuberite Forum
Regular expressions for Lua - Printable Version

+- Cuberite Forum (https://forum.cuberite.org)
+-- Forum: Plugins (https://forum.cuberite.org/forum-1.html)
+--- Forum: Plugin Discussion (https://forum.cuberite.org/forum-8.html)
+--- Thread: Regular expressions for Lua (/thread-2163.html)



Regular expressions for Lua - NiLSPACE - 10-13-2015

In my internship I've come across regular expressions allot, and I now realize how powerful they are compared to Lua patterns. Lua patterns suffice for almost any occasion, but it might scare developers away when they learn that Lua doesn't have Regex.

I thought we could add some really simple regex support. Just by putting a few functions in a table that basically calls preg_XYZ. I'd do it myself if there are no objections.


RE: Regular expressions for Lua - xoft - 10-14-2015

There's already several Lua libraries for regexp work, LPeg is most likely the best known. I don't think it's worth rolling our own, unless there are reasons to do so.

Also, I don't think it's a good strategy to just bloat the server with all the libraries. It works with LuaRocks and possibly other integration systems, I'd leave it at that. We don't want to maintain a zillion binding libraries.


RE: Regular expressions for Lua - NiLSPACE - 10-14-2015

I was just thinking about something simple like this:
static int tolua_RegexMatch(lua_State * tolua_S)
{
	cLuaState S(tolua_S)
	if (
		!S.CheckParamString(1, 2)
	)
	{
		return 0;
	}
	
	AString str, regexstring, res;
	L.GetStackValues(1, str, regexstring);
	
	std::regex reg(regexstring);
	res = std::regex_match(str, reg);
	
	L.Push(res);
}

///
//...
///

tolua_beginmodule(tolua_S, "cRegex");
	tolua_function(tolua_S, "Match", tolua_RegexMatch);
tolua_endmodule(tolua_S);