Gsub replace with match

I’m in the process of making a string parser to dynamically render strings.

For example the parser would turn “Hello {username}” in “Hello iKingNinja”.

Right now I’m using <string>:gsub() to replace keywords but when the string includes punctuation characters it doesn’t work.

To match punctuation characters I could use %p in the pattern but I also need to add it to the replacement string so for example:
“Hello {username}!” → “Hello iKingNinja!”

The problem is that there are different punctuation characters so I can’t assign a fixed replacement.

I’m looking for a way to match the punctuation character and concatenate it with the replacement while keeping it a simple script.

For example:

local input = "Hello {username}!"
print(input:gsub("{username}%p", "iKingNinja" .. matchedPunctuationChar)
1 Like

I hope this is the script you are looking for:

local s = "Pizza {username}!"

local parsedWord = "{username}"
local replacement = "LakshyaK2011"

local strs = string.split(s, parsedWord)
local final = ""

for i, v in ipairs(strs) do
	if(i >= #strs) then
		final = final..v
	else
		final = final..v..replacement
	end
end
print(final)
1 Like

I’m assuming you want to use RichText and apply it to both the username and the punctuation or just want to modify the punctuation with a lookup table.

This takes a couple of steps.

local userName = "treebee63"
local inputString = "Hello {username}?! Welcome to this place!" -- input string

local userNamePunc = string.match(inputString, "{username}[!?.,]*") -- search for the {username} + punctuation, might contain a punctuation or not
local punctuation = string.match(userNamePunc, "[!?.,]+") -- extract the punctuation by itself in the search

if punctuation then -- if punctuation exists, do something with it
	punctuation = string.rep(punctuation, 3) -- here i just duplicate it 3 times
end

print(string.gsub(inputString, "{username}[!?.,]*", userName .. punctuation))
-- output: Hello treebee63?!?!?! Welcome to this place!

Weird because it works for me?

local str = "Hello, {username}!" 
local punctuation = "!!!!" 
local username = "Kat"
str = string.gsub(str,"{username}%p+",username..punctuation) 
print(str)

image

Here are a couple of alternatives. The first one may be easier to understand. The second one utilizes a string capture. When using the string capture, the replaced string is the string matching the entire \{username\}({punctuationPattern}) pattern but the string given to the function that returns the replacement string is the string matching the pattern punctuationPattern that is in the capture. A string capture is enclosed in parentheses ().

local input: string = "Hello, {username}!!?!"
local userName: string = "RoBoPoJu"

local punctuationPattern: string = "[!?.,]+" -- "%p" would also include }.

-- option 1
local output1: string = string.gsub(
	input,
	`\{username\}{punctuationPattern}`,
	function(found: string): string
		local punctuation: string = string.gsub(found, "{username}", "")
		return userName .. punctuation
	end
)

-- option 2
local output2: string = string.gsub(
	input,
	`\{username\}({punctuationPattern})`,
	function(punctuation: string): string
		return userName .. punctuation
	end
)

print(output1) -- Hello, RoBoPoJu!!?!
print(output2) -- Hello, RoBoPoJu!!?!

Why do you even need to include punctuation in the match, though?