Construct "or patterns" with glue
In the last two lessons you learned to create strings from other strings or vectors or lists, even data frames. Together with the knowledge you have about regular expressions, you are now able to create patterns for all these data types.
Using glue_collapse()
you can concatenate the contents of a vector of the column of a data frame and create long patterns that would otherwise be very tedious and error prone to write from hand.
For this exercise, we have a vector users
as an input. It is the result of a database export. It contains some rows of a database but also some other information that we don't care about. Using regular expressions we can match only the parts that we are interested in, in this case, the usernames.
This exercise is part of the course
Intermediate Regular Expressions in R
Exercise instructions
- Use the vector
usernames
to create a pattern that matches either of the three names in the vector. - Bind the three names together using the regular expression "or" character as a separator.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
usernames <- c("Bryan", "Barbara", "Tom")
# Create a pattern using the vector above separated by "or"s
user_pattern <- glue_collapse(___, sep = "___")
str_view(users, user_pattern)