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
Returns the text of the capture group with the specific `group` index or name, or NULL otherwise. Errors if `pattern` is not legal regex. Based on [`Regex.captures()`](https://docs.rs/regex/latest/regex/struct.Regex.html#method.captures).
109
+
110
+
If `group` is a number, then the N-th capture group is returned, where `0` refers to the entire match, `1` refers to the first left-most capture group in the match, `2` the second, and so on. If the provided group number "overflows', then NULL is returned.
111
+
112
+
```sql
113
+
select regex_capture(
114
+
"'(?P<title>[^']+)'\s+\((?P<year>\d{4})\)",
115
+
"Not my favorite movie: 'Citizen Kane' (1941).",
116
+
0
117
+
);
118
+
-- "'Citizen Kane' (1941)"
119
+
120
+
select regex_capture(
121
+
"'(?P<title>[^']+)'\s+\((?P<year>\d{4})\)",
122
+
"Not my favorite movie: 'Citizen Kane' (1941).",
123
+
1
124
+
);
125
+
-- "Citizen Kane"
126
+
127
+
128
+
select regex_capture(
129
+
"'(?P<title>[^']+)'\s+\((?P<year>\d{4})\)",
130
+
"Not my favorite movie: 'Citizen Kane' (1941).",
131
+
2
132
+
);
133
+
-- "1941"
134
+
135
+
select regex_capture(
136
+
"'(?P<title>[^']+)'\s+\((?P<year>\d{4})\)",
137
+
"Not my favorite movie: 'Citizen Kane' (1941).",
138
+
3
139
+
);
140
+
-- NULL
141
+
```
142
+
143
+
If group is a string, then the value of the capture group with the same name is returned. If there is no matching capture group with the name, or the group was not captured, then NULL is returned.
144
+
145
+
```sql
146
+
select regex_capture(
147
+
"'(?P<title>[^']+)'\s+\((?P<year>\d{4})\)",
148
+
"Not my favorite movie: 'Citizen Kane' (1941).",
149
+
'title'
150
+
);
151
+
-- "Citizen Kane"
152
+
153
+
154
+
select regex_capture(
155
+
"'(?P<title>[^']+)'\s+\((?P<year>\d{4})\)",
156
+
"Not my favorite movie: 'Citizen Kane' (1941).",
157
+
'year'
158
+
);
159
+
-- "1941"
160
+
161
+
select regex_capture(
162
+
"'(?P<title>[^']+)'\s+\((?P<year>\d{4})\)",
163
+
"Not my favorite movie: 'Citizen Kane' (1941).",
164
+
'not_exist'
165
+
);
166
+
-- NULL
167
+
```
168
+
169
+
Note that there is a version of `regex_capture()` that only have two parameters: `captures` and `group`. This can only be used with the [`regex_captures`](#regex_captures) table function, with the special `captures` column like so:
"'Citizen Kane' (1941), 'The Wizard of Oz' (1939), 'M' (1931)."
179
+
);
180
+
/*
181
+
┌──────────────────┬──────┬───────────┐
182
+
│ title │ year │ not_exist │
183
+
├──────────────────┼──────┼───────────┤
184
+
│ Citizen Kane │ 1941 │ │
185
+
│ The Wizard of Oz │ 1939 │ │
186
+
│ M │ 1931 │ │
187
+
└──────────────────┴──────┴───────────┘
188
+
*/
189
+
```
190
+
191
+
<h3name="regex_captures"><code>select * from regex_captures(pattern, text)</code></h3>
192
+
193
+
Returns all non-overlapping capture groups in the given text. Similar to [`regex_find_all`](#regex_find_all), but allows for extracting capture information. Must use with the [`regex_capture`](#regex_capture) function to extract capture group values. Based on [`Regex.captures_iter()`](https://docs.rs/regex/latest/regex/struct.Regex.html#method.captures_iter).
194
+
195
+
The returned columns:
196
+
197
+
-`rowid`: The 0-based index of the match. `0` is the entire match, `1` the first matching capture group, `2` the second, etc.
198
+
-`captures`: A special value that's meant to be passed into [`regex_capture()`](#regex_capture). Will appear NULL through direct access.
199
+
200
+
For faster results, wrap the pattern with the [`regex()`](#regex) function for caching.
Replace the **first** instance of `pattern` inside `text` with the given `replacement` text. Supports the [replacment string syntax](https://docs.rs/regex/latest/regex/struct.Regex.html#replacement-string-syntax). Based on [`Regex.replace()`](https://docs.rs/regex/latest/regex/struct.Regex.html#method.replace)
0 commit comments