Lua: Find string between two strings

Viewed 561
local code = [[
    print("this is a trap")    
    asd("XDasdsadasdasd")
    print("this is a trap")
]]
print("\n\n\n\n\n")
print(string.match(code, 'asd(.*)'))

I made this but the problem is it also returns the print under it. It'll return anything under the asd("XDasdsadasdasd"), but i only want whats inside asd("XDasdsadasdasd") to return that will be "XDasdsadasdasd"

1 Answers

Parentheses are magic chars in Lua patterns. You need to escape them. Also, you need to stop at the first closing parenthesis. See the code below:

print(string.match(code, 'asd%(.-%)'))

If you only want what's inside asd(...), then use

print(string.match(code, 'asd%((.-)%)'))
Related