Skip to content

Commit 94a8075

Browse files
committed
main
1 parent 75682d7 commit 94a8075

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

docs/.vitepress/config.mts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ const vitepressConfig = defineConfig({
1818
lineNumbers: true,
1919
theme: {
2020
light: await themeService.getTheme('Eva Light'),
21-
// dark: await themeService.getTheme('Eva Dark'),
21+
dark: await themeService.getTheme('Eva Dark'),
2222
// light: await themeService.getTheme('JetBrains Rider New UI theme - Light'),
23-
dark: await themeService.getTheme('JetBrains Rider New UI theme - Dark'),
23+
// dark: await themeService.getTheme('JetBrains Rider New UI theme - Dark'),
2424
},
2525
codeTransformers: [transformerTwoslash()],
2626
},

docs/document/Lua/docs/String.md

Lines changed: 54 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
# String
22

3+
> [!NOTE]
4+
> All `pattern` parameter in lua string library accepts `number` which would be converted as ASCII
5+
36
## Pattern Matching
47

58
### Range of Occurrence
69

7-
- parameters
10+
- **parameters**
811
- `pattern: string | number`: pattern to find, may include capture groups
912
- `init?: int`: the index to start searching
1013
- `plain?: boolean`: whether to match in literal
1114

12-
- returns
15+
- **returns**
1316
- `start`: start index of the first match on **the whole pattern**
1417
- `tail`: start index of the first match on **the whole pattern**(do not use `end` as variable name, it's reserved keyword)
1518
- `...matches`: arbitrary number of captured groups that can be unpacked to variables
@@ -19,22 +22,22 @@ local text = '<tag>content</tag>'
1922

2023
-- find range of first occurrence
2124
-- and its captured groups, kind of useless though
22-
local start, tail, tag, content = text:find('<(%w+)>(%w+)</%w+>')
25+
local start, tail, tag, content = text:find('abc<(%w+)>(%w+)</%w+>')
2326
```
2427

2528
### Replace
2629

2730
`string.gsub`: replace all occurrences
2831

29-
- parameters
32+
- **parameters**
3033
- `pattern: string | number`: pattern to replace
3134
- `repl: string | number | function | table`
3235
- `string`: may use `%n` to reference capture groups by index `n` to transform from groups
33-
- `fun(match: string): string`: to replace the occurrence by tranformed string from the match
34-
- `table`: a finite pairs for match(key) and replacement(value)
36+
- `fun(match: string): string`: to replace the occurrence by tranformed string from the match(`match` parameter is just the first match)
37+
- `table<string, string>`: a finite pairs for match(key) and replacement(value)
3538
- `n?: int`: max count of substitutions could be made
3639

37-
- returns
40+
- **returns**
3841
- `text`: substituted string
3942
- `count`: substitution count made
4043

@@ -59,3 +62,47 @@ local sub, count = text:gsub('%l+', {
5962
}) -- python_case_is_awful, 4
6063
-- because the table were tried anyway and got nil when key is not registered
6164
```
65+
66+
### Get Captured Groups
67+
68+
#### Match for One time
69+
70+
- **parameters**
71+
- `pattern`: pattern to match that could have multiple captured groups
72+
- `init?: int`: index to start matching
73+
74+
- **returns**
75+
- `...matches`: arbitrary number of matches that can be unpacked
76+
77+
```lua
78+
local text = 'John Smith is not me'
79+
80+
-- only match on one time
81+
local first_name, last_name = text:match('(%w+) (%w+)') -- John, Smith
82+
```
83+
84+
#### Match for Whole String
85+
86+
`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.
87+
88+
- **parameters**
89+
- `pattern`: pattern to match, may include captured groups
90+
91+
- **returns**
92+
- `...matchs`: arbitrary number of captured groups
93+
94+
```lua
95+
local text = 'John Smith is not me'
96+
97+
for first_name, last_name in text:gmatch('(%w+) (%w+)') do
98+
print(first_name)
99+
print(last_name)
100+
end
101+
-- John
102+
-- Smith
103+
-- is
104+
-- not
105+
```
106+
107+
> [!CAUTION]
108+
> Do not use `^` in pattern for `string.gmatch`, it terminates the iteration from the beginning

0 commit comments

Comments
 (0)