library(stringr)<- c("The cat and the hat", "an apple at the table") x
Match Any Character with a Wildcard
The dot .
is a wildcard that matches any single character, e.g., ....
represents any four consecutive characters, and .*
represents a string of any length (including an empty string; recall that the quantifier asterisk represents zero or more of the immediately preceding character). Consider the following examples.
eg.1. ...at
matches any five characters (including whitespace) that ends with “at”.
str_view_all(x, "...at")
Output:
[1] │ Th<e cat> and th<e hat>
[2] │ an app<le at> the table
str_extract_all(x, "...at", simplify = T)
Output:
[,1] [,2]
[1,] "e cat" "e hat"
[2,] "le at" ""
eg.2. @.*
extracts “@” and all the following characters, and returns the email domain names.
<- c("david.doxey@harvard.edu", e "young_mike@cisco.com")
str_view(e, "@.*")
Output:
[1] │ david.doxey<@harvard.edu>
[2] │ young_mike<@cisco.com>
str_extract(e, "@.*")
Output:
[1] "@harvard.edu" "@cisco.com"