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
Parenthesis also define "groups" that you can refer to with __backreferences__, like `\1`, `\2` etc, and can be extracted with `str_match()`. For example, the following regular expression finds all fruits that have a repeated pair of letters:
252
+
Parentheses also define "groups" that you can refer to with __backreferences__, like `\1`, `\2` etc, and can be extracted with `str_match()`. For example, the following regular expression finds all fruits that have a repeated pair of letters:
This is most useful for more complex cases where you need to capture matches and control precedence independently.
272
272
273
+
You can use `(?<name>...)`, the named capture group, to provide a reference to the matched text. This is more readable and maintainable, especially with complex regular expressions, because you can reference the matched text by name instead of a potentially confusing numerical index.
274
+
275
+
*Note: `<name>` should not include an underscore because they are not supported.*
You can then use `\k<name>` to backreference the previously captured named group. It is an alternative to the standard numbered backreferences like `\1` or `\2`.
284
+
285
+
```{r}
286
+
text <- "This is is a test test with duplicates duplicates"
287
+
pattern <- "(?<word>\\b\\w+\\b)\\s+\\k<word>"
288
+
str_subset(text, pattern)
289
+
str_match_all(text, pattern)
290
+
```
291
+
273
292
## Anchors
274
293
275
294
By default, regular expressions will match any part of a string. It's often useful to __anchor__ the regular expression so that it matches from the start or end of the string:
0 commit comments