How to format words, tags and in gsub?

Hello,
I rephrased my question, as the previous one was pretty messed up.
What I want is that, using patterns, the text looks like this.

local ckey = {
	
	["Hello"] 	= "-=[Hello]";
	["World"]	= "[World]=-";
	["a"]	    = "--.--";
	
	['<'] 		= ">>";
	['>'] 		= "<<";
	
	['0']       = "ZERO";
	
}

print(string.gsub("Hello <a0a> World", "[%p%w+]", ckey))

Unwanted result:

Hello >>–.–ZERO–.–<< World

Desired result:

-=[Hello] >>–.–ZERO–.–<< [World]=-

Thanks in advance.

There isn’t a format for the digit 0, while there is one for 1, 2, 3

Hi, I rephrased my question, if you can help I appreciate it.

I have written a function that searches over the string, replacing matches when it finds them:

local ckey = {

	["Hello"] 	= "-=[Hello]";
	["World"]	= "[World]=-";
	["a"]	    = "--.--";

	['<'] 		= ">>";
	['>'] 		= "<<";

	['0']       = "ZERO";

}

local read = ""
local function is_match(str, key)
	for i,v in pairs(key) do
		if i == str then
			return v
		elseif i:match("^"..str) then
			read = str
			return true
		end
	end
	read = ""
end
local function replace(str, key)
	local rep = ""
	for i=1,str:len() do
		local result = is_match(read .. str:sub(i, i), key)
		rep = rep .. (not result and str:sub(i, i) or type(result) == "string" and result or "")
	end
	return rep
end

print(replace("Hello <a0a> World", ckey))
1 Like

Use 2 gsubs instead

local ckey = {
	["Hello"]	= "-=[Hello]",
	["World"]	= "[World]=-",
	["a"]		= "--.--",
	
	['<']		= ">>",
	['>']		= "<<",
	
	['0']		= "ZERO"
}

local myString = "Hello <a0a> World"
myString = string.gsub(myString, ".", ckey) -- Check every letter
myString = string.gsub(myString, "%S+", ckey) -- Check every long letter that is not a space
print(myString)

JayzYeah32’s script works, but it starts breaking when you have words that are almost the same, like “Hellooo” or “H”.

1 Like

Thank you brother. I’m learning about patterns, it’s very confusing.

Thanks for your time brother. I’m learning about patterns, it’s very confusing.