Reputation: 38
I'm having trouble with my regular expression not working in Lua. I have tested it in other environments like my text editor and some online regex tools and it seems to work fine there.
Regex that works how I want it to, in other environments:
RAY\.decrypt\s*\(([\"\'].+?(?=[\"\']).+?(?=[\)]))\s*\)
Regex that I'm attempting to use in Lua (just \'s replaced with %'s)
RAY%.decrypt%s*%(([\"\'].+?(?=[\"\']).+?(?=[%)]))%s*%)
Sample text that I'm trying to match (I want to capture the content in the parentheses)
RAY.decrypt ("\x02\x02\x02\x02\x02\x02\x02\x02")
RAY.decrypt ("\xd6E\xd6E\xd6E\xd6E\xd6E")
RAY.decrypt("\x8e\x8e\x8e\x8e\x8e\x8e\x8e\x8e")
Other tools match the text and capture what I want it to capture, perfectly. But I'm struggling with making Lua do it as it doesn't match anything.
local s = [[RAY.decrypt ("\x02\x02\x02\x02\x02\x02\x02\x02")
RAY.decrypt ("\xd6E\xd6E\xd6E\xd6E\xd6E")
RAY.decrypt("\x8e\x8e\x8e\x8e\x8e\x8e\x8e\x8e")]]
print(string.find(s, "RAY%.decrypt%s*%(([\"\'].+?(?=[\"\']).+?(?=[%)]))%s*%)"))
> nil
Any help would be appreciated, thanks!
Upvotes: 0
Views: 393
Reputation: 1
Lua built-in "regex" are called "patterns"
This matchs content in the parentheses
local source = [[
RAY.decrypt ("\x02\x02\x02\x02\x02\x02\x02\x02")
RAY.decrypt ("\xd6E\xd6E\xd6E\xd6E\xd6E")
RAY.decrypt("\x8e\x8e\x8e\x8e\x8e\x8e\x8e\x8e")
]]
-- this captures expressions containing RAY.decrypt ( " ... " )
for content in string.gmatch(source , "RAY%.decrypt%s*%(%s*\"(.-)\"%s*%)") do
print(content)
end
-- this captures expressions inside double quotes
for content in string.gmatch(source , "\"(.-)\"") do
print(content)
end
-- this captures expressions inside single or double quotes e.g. RAY.decrypt ( '\x8e\x8e\x8e\x8e\x8e\x8e\x8e\x8e')
for content in string.gmatch(source , "[\"'](.-)[\"']") do
print(content)
end
-- this captures expressions inside parentheses (will capture the quotes)
for content in string.gmatch(source , "%((.-)%)") do
print(content)
end
Upvotes: 0
Reputation: 72312
Lua's standard string library does not support full regular expressions but its pattern matching is quite powerful.
This script matches everything inside parentheses:
for w in s:gmatch("%((.-)%)") do
print(w)
end
Upvotes: 0
Reputation: 23737
local s = [[
RAY.decrypt ("\x02\x02\x02\x02\x02\x02\x02\x02")
RAY.decrypt ('\xd6E\xd6E\xd6E\xd6E\xd6E')
RAY.decrypt( "\x8e\x8e\x8e\x8e\x8e\x8e\x8e\x8e" )
]]
for w in s:gmatch"RAY%.decrypt%s*%(%s*(([\"']).-%2)%s*%)" do
print(w)
end
Output:
"\x02\x02\x02\x02\x02\x02\x02\x02"
'\xd6E\xd6E\xd6E\xd6E\xd6E'
"\x8e\x8e\x8e\x8e\x8e\x8e\x8e\x8e"
Upvotes: 1
Reputation: 1431
I don't know LUA at all, but wikipedia says about the LUA regex engine: "Uses simplified, limited dialect; can be bound to more powerful library, like PCRE or an alternative parser like LPeg."
So you should either bind it to PCRE, or you shouldn't compare to other engines and just use the LUA documentation to write the regex specifically for LUA, not your text editor.
Upvotes: 0