Homeworld 2 : Functiongsub
Karos Graveyard ::
Function Reference :: Scope Reference :: Variable Reference
gsub(<sString>, <sPattern>, <Replacement> [, <iLimit>])

Description (from LUA 4 Manual: 6.2 String Manipulation
"Returns a copy of <sString> in which all occurrences of the pattern <sPattern> have been replaced by a replacement string specified by <Replacement>. gsub also returns, as a second value, the total number of substitutions made.
If <Replacement> is a string, then its value is used for replacement. Any sequence in <Replacement> of the form %n with n between 1 and 9 stands for the value of the n-th captured substring.
If <Replacement> is a function, then this function is called every time a match occurs, with all captured substrings passed as arguments, in order (see below). If the value returned by this function is a string, then it is used as the replacement string; otherwise, the replacement string is the empty string.
The last, optional parameter <iLimit> limits the maximum number of substitutions to occur. For instance, when <iLimit> is 1 only the first occurrence of <sPattern> is replaced."

Example (from LUA 4 Manual: 6.2 String Manipulation
x = gsub("hello world", "(%w+)", "%1 %1")
--> x="hello hello world world"

x = gsub("hello world", "(%w+)", "%1 %1", 1)
--> x="hello hello world"

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

x = gsub("home = $HOME, user = $USER", "%$(%w+)", getenv)
--> x="home = /home/roberto, user = roberto" (for instance)

x = gsub("4+5 = $return 4+5$", "%$(.-)%$", dostring)
--> x="4+5 = 9"

local t = {name="lua", version="4.0"}
x = gsub("$name - $version", "%$(%w+)", function (v) return %t[v] end)
--> x="lua - 4.0"

t = {n=0}
gsub("first second word", "(%w+)", function (w) tinsert(%t, w) end)
--> t={"first", "second", "word"; n=3}

Arguments
<sString>: the string to search in.
<sPattern>: the pattern to search for.
<Replacement>: the pattern used as a replacement for <sPattern>. (May be a string or a function; see Description.)
<iLimit>: a limit on the number of replacements that will be made.

Related Functions
:: ::
Hide Download Bar
Ctrl + Shift + Z = Show/Hide Download Bar
Downloads
Download Manager (S3)
Clear
Left Click: Clear All Finished
Right Click: Undo last clear
100%
Download History
Download list is empty
FunctionGetMultiplierForExtraShips.html
100%
00:00
496 KB
2.17 MB/s
FunctionGetMultiplierForExtraShips.html
Source:
From:
To:
G:\HWRMResources\tutorials\hw2wiki.net\Functions_StandAlone\FunctionGetMultiplierForExtraShips.html
Status:
496 KB / 496 KB ( 2.17 MB/s )
File Size:
496 KB
Virus scan:
Time Start:
2019.08.21 - 23:13:22
Time to Complete:
<00:01
Time Left:
00:00
Percent Done:
100%
Average Speed:
2.35 MB/s
mouse double click: download pause/resume
mouse middle click: download cancel
mouse double click: open file
mouse double click + Ctrl: open dir
mouse middle click: delete from list
mouse middle click + Ctrl: delete from system
press mouse button + Ctrl: drag & drop
FunctionGetMultiplierForExtraRU.html
100%
00:00
495 KB
2.44 MB/s
FunctionGetMultiplierForExtraRU.html
Source:
From:
To:
G:\HWRMResources\tutorials\hw2wiki.net\Functions_StandAlone\FunctionGetMultiplierForExtraRU.html
Status:
495 KB / 495 KB ( 2.44 MB/s )
File Size:
495 KB
Virus scan:
Time Start:
2019.08.21 - 23:13:30
Time to Complete:
<00:01
Time Left:
00:00
Percent Done:
100%
Average Speed:
2.70 MB/s
mouse double click: download pause/resume
mouse middle click: download cancel
mouse double click: open file
mouse double click + Ctrl: open dir
mouse middle click: delete from list
mouse middle click + Ctrl: delete from system
press mouse button + Ctrl: drag & drop