0% 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.
Copyright
© © All Rights Reserved
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% 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.
Copyright
© © All Rights Reserved
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)


return load(s)()
end)

print(x)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy