Created
June 28, 2021 20:40
-
-
Save luisDVA/36a763f6d4974105b30cf5f6bf06da6f to your computer and use it in GitHub Desktop.
regex in R yt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##%######################################################%## | |
# # | |
#### Regex in R - Your Turn #### | |
# # | |
##%######################################################%## | |
# Match the following regular expressions against the test vector below using `str_detect`. | |
## Can you explain the matches? | |
# Regular expressions | |
# 1. ^dog | |
# 2. ^[a-z]+$ | |
# 3. \\d | |
test_vector <- c("Those dogs are small.","dogs and cats", | |
"34","(34)","rat","watchdog","placemat", | |
"BABY","2011_April","mice") | |
# load packages ----------------------------------------------------------- | |
library(stringr) | |
# create text string ------------------------------------------------------ | |
test_vector <- c("Those dogs are small.","dogs and cats", | |
"34","(34)","rat","watchdog","placemat", | |
"BABY","2011_April","mice") | |
# evaluate if each element contains a match ------------------------------- | |
# match the literal string dog | |
str_detect(test_vector,"^dog") # second element contains match | |
# match lowercase | |
str_detect(test_vector,"^[a-z]+$") # elements 5,6,7, and 10 matched | |
# match numbers | |
str_detect(test_vector,"\\d") # elements 3,4, and 9 matched |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment