MSCS Happenings
Looking Ahead to Registration
Projects
Assignments
Tidy Tuesday
Iterative Viz
Regular expressions (regex) allow us to describe character patterns.
After class, try: Interactive Regex Tutorial
For now, we’ll use our cheatsheet (back is Regular Expressions)!
[1] "The quick brown fox jumps over the lazy dog."
We’ll practice:
To search for a pattern and replace it, we can use the functions str_replace
and str_replace_all
.
[1] "The really quick brown fox jumps over the lazy dog."
example2 <- "Two roads diverged in a yellow wood, / And sorry I could not travel both / And be one traveler, long I stood / And looked down one as far as I could"
example3 <- "This is a test"
(examples <- c(example, example2, example3))
[1] "The quick brown fox jumps over the lazy dog."
[2] "Two roads diverged in a yellow wood, / And sorry I could not travel both / And be one traveler, long I stood / And looked down one as far as I could"
[3] "This is a test"
[1] "The quick brown fox jumps over the lazy dog."
start end
[1,] 23 25
Let’s check the answer:
[1] "Two roads diverged in a yellow wood, / And sorry I could not travel both / And be one traveler, long I stood / And looked down one as far as I could"
pat2 <- "[^aeiouAEIOU ][aeiouAEIOU]{2}[^aeiouAEIOU ]{1}" # consonant followed by two vowels followed by a consonant
str_extract(example2, pat2) # extract first match
[1] "road"
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] "road" "wood" "coul" "tood" "look" "coul"
[1] "Two roads diverged in a yellow wood, / And sorry I could not travel both / And be one traveler, long I stood / And looked down one as far as I could"
[1] "two roads diverged in a yellow wood, / and sorry i could not travel both / and be one traveler, long i stood / and looked down one as far as i could"
Go to our course website and create a new Rmd template file, save it in a folder called Assignment_09
.
Regular Expressions
Other Assignments