Static analyzer gives incorrect "invalid match pattern" for matches with double-percent signs

The following regexes show me static analyzer warnings, but they’re functioning exactly as intended (both on roblox & on Lua: demo).

  • “([&$%%].)”: capture an &, $, or % followed by any character.
  • “(%%%d*%.?%d*)([a-zA-Z%%])”: capture a C-style “printf” specifier.
  • “[%%]”: minimal capture string which causes this issue.

You can reproduce by creating a new place, throwing a script in the workspace & pasting these contents into it:

lua script
local secretDecoderRing = {
	["&0"] = "foo";
	["%1"] = "bar";
	["$2"] = "baz";
	--etc.
};
local text = "&0 then %1 then $2";
print((string.gsub(text, "([&$%%].)", function(c) return secretDecoderRing[c]; end)));
--> foo then bar then baz

local text = "Hello from %30.4s"
for style, formatType in string.gmatch(text, "(%d*%.?%d*)([a-zA-Z])") do
	print(style, formatType);
end
-->%30.4	s 
Screenshot

This is a problem with character sets where it thinks the second % escapes the ending ].

Also, Lua doesn’t use regular expressions, it uses it’s own pattern matching which is quite different.

1 Like