Skip to content

Instantly share code, notes, and snippets.

@toyeiei
Last active Feb 4, 2019
Embed
What would you like to do?
# intro to stringr
install.packages("stringr")
library(stringr)
# check state.name
print(state.name)
# most used function in stringr
str_view(state.name, pattern = "New", match = T)
# create an example string
text <- "DataRockie is the coolest DS school on the internet, founded in Y2015.
It offers free online courses on datarockie.com, go check it!"
# basic regular expressions
str_view(text, "DataRockie")
str_view(text, "2015")
str_view(text, "datarockie")
# this character | means OR
# look for words 'DataRockie' OR 'datarockie'
str_view_all(text, "(D|d)ata(R|r)ockie")
# look for digits
str_view(text, "[0-9]")
str_view(text, "[0-9]+")
str_view(text, "\\d")
str_view(text, "\\d+")
# look for English alphabet
str_view_all(text, "[a-z]+")
str_view_all(text, "[A-Z]+")
# look for space
str_view_all(text, "\\s")
# split text by " "
text_split <- str_split(text, pattern = " ")[[1]]
text_split
# look for words start with 'i'
str_view_all(text_split, "^i.+")
# look for words end with 's'
str_view_all(text_split, ".+s$")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment