Regular Expressions


In this lesson, you will learn to:


Time Estimates:
     Videos: 20 min
     Readings: 0-20 min
     Activities: 90 min
     Check-ins: 4



Regular Expressions


Required Video: Regular Expressions




Recommended Reading: R4DS: String Matching with Regular Expressions




Check-In 1: A few more symbols


Question 1:

Recall that the regular expression [abc] matches the characters a, b, or c.

What does [^abc] match?

Question 2:

When it is not inside square brackets, the ^ symbol means “start of string”.

What will be returned by the following?

my_str <- c("Kelly", "Hi Kelly", "Kelly Bodwin", "Who is Kelly?")
str_subset(my_str, "^Kelly")

Question 3:

THe $ symbol in a regular expression means “end of string”.

What will be returned by the following?

my_str <- c("Kelly", "Hi Kelly", "Kelly Bodwin", "Who is Kelly?")
str_subset(my_str, "Kelly$")


Check-In 2: Simple Regular Expressions


What will the following outputs be?

my_str <- "The Dursleys of Number 4 Privet Drive were happy to say that they were perfectly normal, thank you very much."

str_extract_all(my_str, ".*")

str_extract_all(my_str, "\\w")

str_extract_all(my_str, "\\s")

str_extract_all(my_str, "[:alpha:]+")

str_extract_all(my_str, "[:alpha:]*\\.")

str_extract_all(my_str, "[wv]er[ey]")