Having trouble using string.gsub() with table replacement

Hello! I am tying to use string.gsub() with a table of key value pairs to replace pieces of strings
within a longer string.

So far, the issue I’m having is that the string isn’t being replaced. The string I want replaced are “” and “” which I have as keys in the the table.

How would I go about replacing these characters with other strings using a table that holds the
characters to be replaced as keys and substitutions as values? I also would want to replace “<m#NUMBER>” & “</m#NUMBER>”, with “#NUMBER” being an actual integer (i.e , , , , etc… )

Thanks in advance

Example of issue

local myString = "Hello! My name is <m>John Doe</m>" 
local patternReplaceTable = { 
    ["<m>"] = "[RED]";
    ["</m>"] = "[/RED]";
}
local formattedString = string.gsub(myString, "%w+" , patternReplaceTable)
print(formattedString) -- would return the same string

Hello!

It doesn’t replace it, because the pattern %w+ only recognizes letters or numbers, however, < and > are punctuation characters, thereby %p is used.

But, you see, the whole thing is a little bit more complex, since %p+ wouldn’t capture any letter between the <>. A Pattern what I found working was <[^%s]+>. This will capture the <, every literal which is not a whitespace, and the >. This should also get “#NUMBER”. If you want to read up on String Patterns, I will reference to the Roblox Guide, since they can be really useful when used correctly.

However, the patternReplaceTable will still not recognize the “#NUMBER”. Luckily, the table can be replaced by a function like this:

local formattedString = string.gsub(myString, "<[^%s]+>", function(text)
	local replacement = ""
	
	-- Do replacing here
	
	return replacement
end)

There you can process further.

Hope I could help!

1 Like

Hi. Thank you for informing me about the pattern character classes.

NOTE:
For the initial post, the “” were meant to be "<m>" & "</m>" & (i.e , , ,) was meant to be (i.e <m1>, </m1>, <m2> </m2>, etc). I didn’t know the forum would make them invisible.

So far your method does work for the given example string, "Hello! My name is <m>John Doe</m>". But I think the pattern doesn’t cover for when the tags "<m>" & "</m>" are followed by numbers. For example, the string "Gained <m>10</m> experience points" would return "<m>10</m>" as the txt parameter rather than <m> & </m>.

Code show casing issue:

local markupBegin = [[<font color = "rgb(255,0,0)">]]
local markupEnd = [[</font>]]
local myString = "Gained <m>10</m> experience points" 
local changeTable = {
    ["<m1>"] = markupBegin;
    ["</m1>"] = markupEnd;
}

string.gsub(myString,"<[^%s]+>", function(txt)
    local replacement = ""

    print(txt) -- Would return <m>10</m> instead of <m> & </m> seperate 


    return replacement
end)

--[[
Goal would be to use string.gsub to get the following string;
"Gained <font color = "rgb(255,0,0)">10</font> experience points". 
]]

I’ll continue to mess around with the character class combinations, but anyone knows of one that will cover this issue please let me know.

Thanks again.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.