본문으로 이동

모듈:string/patternEscape

위키낱말사전, 말과 글의 누리

이 모듈에 대한 설명문서는 모듈:string/patternEscape/설명문서에서 만들 수 있습니다

local gsub = string.gsub

local chars
local function get_chars()
	chars, get_chars = {
		["\000"] = "%z", ["$"] = "%$", ["%"] = "%%", ["("] = "%(", [")"] = "%)",
		["*"] = "%*", ["+"] = "%+", ["-"] = "%-", ["."] = "%.", ["?"] = "%?",
		["["] = "%[", ["]"] = "%]", ["^"] = "%^",
	}, nil
	return chars
end

--[==[Escapes the magic characters used in a pattern: {$%()*+-.?[]^}, and converts the null character to {%z}. For example, {"^$()%.[]*+-?\0"} becomes {"%^%$%(%)%%%.%[%]%*%+%-%?%z"}. This is necessary when constructing a pattern involving arbitrary text (e.g. from user input).]==]
return function(str)
	return (gsub(str, "[%z$%%()*+%-.?[%]^]", chars or get_chars()))
end