@@ -38,12 +38,15 @@ library(tidyverse)
3838~~~
3939
4040The grep equivalent in 'stringr' is ` str_detect ` . It's intended for use in searching vectors of strings.
41+
4142``` str_detect( string.vector, 'pattern' ) ```
43+
4244Basic use returns a list of TRUE/FALSE for which vector entries matched the search pattern.
4345
44- Note that you'll need to use double-backslashes in place of any single backslash in R.
46+ ** Note that you'll need to use double-backslashes in place of any single backslash in R.**
4547
4648For example, find words where the same letter is repeated consecutively:
49+
4750~~~ r
4851words <- c(" apple" , " banana" , " carrot" )
4952str_detect( words , ' (\\ w)\\ 1' )
@@ -54,7 +57,8 @@ str_detect( words, '(\\w)\\1' )
5457~~~
5558
5659
57- Applying the output of str_detect back to the original string vector, as indices, allows access to the actual matched entries:
60+ Applying the output of str_detect back to the original string vector, as indices, allows access to the actual matched entries:
61+
5862~~~ r
5963words [ str_detect( words , ' (\\ w)\\ 1' ) ]
6064~~~
@@ -132,9 +136,12 @@ framed_fruit %>%
132136
133137
134138---
139+
135140
136141Substitution in R is enabled through the 'str_replace' or 'str_replace_all' functions.
142+
137143``` str_replace( string.vector, 'pattern', 'replacement' ) ```
144+
138145These functions will complete substitutions in all matching entries in a string vector.
139146The difference is that 'str_replace_all' will also perform greedy matching and replacing within
140147individual vector entries, where 'str_replace' will only replace the first match in each vector
0 commit comments