String โ
NOTE
All pattern parameter in lua string library accepts number which would be converted as ASCII
Pattern Matching โ
Range of Occurrence โ
parameters
pattern: string | number: pattern to find, may include capture groupsinit?: int: the index to start searchingplain?: boolean: whether to match in literal
returns
start: start index of the first match on the whole patterntail: start index of the first match on the whole pattern(do not useendas variable name, it's reserved keyword)...matches: arbitrary number of captured groups that can be unpacked to variables
lua
local text = '<tag>content</tag>'
-- find range of first occurrence
-- and its captured groups, kind of useless though
local start, tail, tag, content = text:find('<(%w+)>(%w+)</%w+>')1
2
3
4
5
2
3
4
5
Replace โ
string.gsub: replace all occurrences
parameters
pattern: string | number: pattern to replacerepl: string | number | function | tablestring: may use%nto reference capture groups by indexnto transform from groupsfun(...matches: string): string: to replace the occurrence by tranformed string from the matchtable<string, string>: a finite pairs for match(key) and replacement(value)
n?: int: max count of substitutions could be made
returns
text: substituted stringcount: substitution count made
lua
local text = 'snake_case_is_awful'
local sub, count = text:gsub('_', '-') -- snake-case-is-awful, 3
-- transform by captured groups
local sub, count = text:gsub('(%l+)_(%l+)', '%1-%2', 1) -- snake-case_is_awful, 1
-- capitalize word for 2 occurrences
local sub, count = text:gsub('%l+', function(match)
---@cast match string
local cap = match:sub(1, 1):upper()
return cap .. match:sub(2)
end, 2) -- Snake_Case_is_awful, 2
local sub, count = text:gsub('%l+', {
snake = 'python',
_case = '',
}) -- python_case_is_awful, 4
-- because the table were tried anyway and got nil when key is not registered1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Matches on Callback โ
One can access one or more captured groups in callback function of string.gsub
lua
local foo = 'abc efg'
local p = '(%w+) (%w+)'
_, _ = foo:gsub(p, function(match1, match2) -- arbitrary count of matches
print(match1) -- abc
print(match2) -- efg
return 'foo'
end)1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
CAUTION
Do not include ^ in any capture group, it stops matching.
Get Captured Groups โ
Match for One time โ
parameters
pattern: pattern to match that could have multiple captured groupsinit?: int: index to start matching
returns
...matches: arbitrary number of matches that can be unpacked
lua
local text = 'John Smith is not me'
-- only match on one time
local first_name, last_name = text:match('(%w+) (%w+)') -- John, Smith1
2
3
4
2
3
4
Match for Whole String โ
string.gmatch is a counterpart for string.match to match all occurrence for the pattern(no optional init parameter however) as a iterator function that must access through for..in statement.
parameters
pattern: pattern to match, may include captured groups
returns
...matchs: arbitrary number of captured groups
lua
local text = 'John Smith is not me'
for first_name, last_name in text:gmatch('(%w+) (%w+)') do
print(first_name)
print(last_name)
end
-- John
-- Smith
-- is
-- not1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
CAUTION
Do not use ^ in pattern for string.gmatch, it terminates the iteration from the beginning