© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2021
W. YarberryCRAN Recipeshttps://doi.org/10.1007/978-1-4842-6876-6_5

5. Typical Uses

William Yarberry1  
(1)
Kingwood, TX, USA
 

5.1 Test for a Match1

The simplest patterns match exact strings:
x <- c("apple", "banana", "pear")
str_extract(x, "an")
## [1] NA   "an" NA
This syntax is case sensitive:
bananas <- c("banana", "Banana", "BANANA")
str_detect(bananas, "banana")
## [1]  TRUE FALSE FALSE
str_detect(bananas, regex("banana", ignore_case = TRUE))
## [1] TRUE TRUE TRUE
The next step up in complexity is ., which matches any character except a newline:
str_extract(x, ".a.")
## [1] NA    "ban" "ear"
You can allow . to match everything, including \n, by setting dotall = TRUE: ...

Get CRAN Recipes: DPLYR, Stringr, Lubridate, and RegEx in R now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.