Lab 9: Strings and Regex
Build a quote from pieces, then write a few patterns.
Work in a Quarto document. Do 9.1
and 9.2 first. words comes with
stringr.
library(tidyverse)
1. Pieces and replacement
- From
full <- "To be or not to be, that is the question.", usestr_sub()to pull two pieces andstr_c()to join them sowriteLines()prints a two-line quote. - Replace
"back"and"lack"with"foo"in one call. Run it onc("back", "lack")and onc("back", "lack", "black").
2. Regular expressions
Use words. For each item, print the matching words.
- Four-letter words that start with
a. - Words that start with a vowel.
- Words that end in
edbut noteed. - Words that start with three consonants.
- Words that end in a consonant.
- Words that end in
ingorise. - Words with two or more vowel-consonant pairs in a row
(
[aeiou][^aeiou]twice).