local str = "Extract (a(b)c) and ((d)f(g))"; for s in string.gmatch(str, "%b()") do print(s) end local str2 = "Extract [a[b]c] and [[d]f[g]]"; for s in string.gmatch(str2, "%b[]") do print(s) end local str3 = "Extract {a{b}c} and {{d}f{g}}"; for s in string.gmatch(str2, "%b{}") do print(s) end