The document contains examples of using the string.gsub function in Lua to replace substrings in strings with other values including user-defined functions. This allows modifying strings by replacing matches with formatted versions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
8 views2 pages
String Gsub
The document contains examples of using the string.gsub function in Lua to replace substrings in strings with other values including user-defined functions. This allows modifying strings by replacing matches with formatted versions.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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)
II) Num Int (Input (Enter Number) ) #Trying To See If The Number Is Pronic or Not I 1 X 1 For I in Range (1, Num) If I (I+1) Num Print (Num Is Pronic) X+ 1 Break Else Continue If X 1 Print (Number