You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-`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)
35
38
-`n?: int`: max count of substitutions could be made
36
39
37
-
- returns
40
+
-**returns**
38
41
-`text`: substituted string
39
42
-`count`: substitution count made
40
43
@@ -59,3 +62,47 @@ local sub, count = text:gsub('%l+', {
59
62
}) -- python_case_is_awful, 4
60
63
-- because the table were tried anyway and got nil when key is not registered
61
64
```
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
+
localtext='John Smith is not me'
79
+
80
+
-- only match on one time
81
+
localfirst_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
+
localtext='John Smith is not me'
96
+
97
+
forfirst_name, last_nameintext: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