String Gsub

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

x = string.

gsub("hello world", "(%w+)", "%1 %1")


print(x)

local input = "hello world"

x = string.gsub("hello world", "%w+", "%0 %0", 1)


print(x)

x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")


print(x)

local input = "hello world from Lua"


local replaced = string.gsub(input, "(%w+)%s*(%w+)%s*(%w+)%s*(%w+)", "%4 %1 %3 %2")

print(replaced)

local t = {name = "lua", ver = "5.6"}


x = string.gsub("$name-$ver.exe", "%$(%w+)", t)

print(x)

local t = {name = "ReadyPlayerOne", GameLevel = "Infinite", Wins = "Infinite"}


x = string.gsub("\n\nName of Player : $name\n\nPlaying Level : $GameLevel\n\nNumber of
Wins : $Wins", "%$(%w+)", t)

print(x)

-- Mic input from player, using speech to text to capture input


local input = [[
Player found (Water) in the cave,
presence of (CarbonMonoxide) Detected,
is that ok for plants as (Glucose) fuels cellular respiration]]

local function formulaToLaTeX(formula)


local replacements = {
water = "H2O",
["carbonmonoxide"] = "CO",
glucose = "C6H12O6"
}
return string.gsub(formula, "(%w+)", replacements)
end

-- Replace chemical formulas with LaTeX representations


local replaced = string.gsub(input, "(%b())", function(formula)
local cleanedFormula = string.lower(formula:sub(2, -2)) -- Remove parentheses
return formulaToLaTeX(cleanedFormula)
end)

print("\n\n"..replaced)

– Wont work in Roblox, install ZeroBrane Studio link https://studio.zerobrane.com/download?not-this-time

x = string.gsub("(4 * 5) - 1 = $return 4*5-1$", "%$(.-)%$", function (s)


return load(s)()
end)

print(x)

You might also like