
Regex and Text Analysis
Grayson White
Math 241
Week 8 | Spring 2026
Mon Lecture
Wed Lecture
How should we modify the code to locate all the numbers from these lyrics of various songs?
[1] │ But I would walk <500> miles
[2] │ Yeah, <360>. When you're in the mirror, do you like what you see?
[3] │ I have loved you for a <1000> years, I'll love you for a <1000> more
[4] │ Where <2> and <2> always makes a <5>
[5] │ <17>-<38>, ay
[6] │ I'm so <3008>, You so <2>000 and late
But now imagine you had a very long vector and you want to locate any number?
Not a good approach!
[1] │ But I would walk <5><0><0> miles
[2] │ Yeah, <3><6><0>. When you're in the mirror, do you like what you see?
[3] │ I have loved you for a <1><0><0><0> years, I'll love you for a <1><0><0><0> more
[4] │ Where <2> and <2> always makes a <5>
[5] │ <1><7>-<3><8>, ay
[6] │ I'm so <3><0><0><8>, You so <2><0><0><0> and late
[:digit:] is a particular Character Class[1] │ But I would walk <500> miles
[2] │ Yeah, <360>. When you're in the mirror, do you like what you see?
[3] │ I have loved you for a <1000> years, I'll love you for a <1000> more
[4] │ Where <2> and <2> always makes a <5>
[5] │ <17>-<38>, ay
[6] │ I'm so <3008>, You so <2000> and late
+ is a quantifier
+: One or moreYour Mission: In groups of 2 - 3, go through the activity to make a “dictionary” of what you believe each of the regular expressions shown is doing, and how it does that.
When you’ve become confident you know what a certain regexp is doing, add a checkmark in your team’s color to the board.
25:00
RegExplainNow, make cross-group pairings and compare dictionaries. Do you agree on each regexp? If not, try to figure out who of you is correct by testing the regexp on different strings. As a last resort, consult with RegExplain and the Regex Manual linked on the previous slide.
15:00
The str_view_all() is a nice helper function.
Now need to learn functions to take action based on our regular expression pattern matching.
Functions to take action based on our regular expression pattern matching.
Detect pattern with:
str_detect()str_subset()str_count()Extract pattern with:
str_extract() and str_extract_all()Replace pattern with:
str_replace() and str_replace_all()Split pattern:
str_split()# A tibble: 60 × 3
Song Album Lyric
<chr> <chr> <chr>
1 Shake It Off (Taylor's Version) 1989 (Taylor's Version) I stay out too late
2 Shake It Off (Taylor's Version) 1989 (Taylor's Version) Got nothin' in my br…
3 Shake It Off (Taylor's Version) 1989 (Taylor's Version) That's what people s…
4 Shake It Off (Taylor's Version) 1989 (Taylor's Version) That's what people s…
5 Shake It Off (Taylor's Version) 1989 (Taylor's Version) I go on too many dat…
6 Shake It Off (Taylor's Version) 1989 (Taylor's Version) But I can't make the…
7 Shake It Off (Taylor's Version) 1989 (Taylor's Version) At least, that's wha…
8 Shake It Off (Taylor's Version) 1989 (Taylor's Version) That's what people s…
9 Shake It Off (Taylor's Version) 1989 (Taylor's Version) But I keep cruisin'
10 Shake It Off (Taylor's Version) 1989 (Taylor's Version) Can't stop, won't st…
# ℹ 50 more rows
[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
[13] FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE
[25] FALSE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE FALSE
[37] FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE FALSE FALSE FALSE TRUE
[49] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
[1] "But I can't make them stay"
[2] "Baby, I'm just gonna shake, shake, shake, shake, shake"
[3] "Shake it off, I shake it off (Ooh)"
[4] "Heartbreakers gonna break, break, break, break, break"
[5] "And the fakers gonna fake, fake, fake, fake, fake"
[6] "Baby, I'm just gonna shake, shake, shake, shake, shake"
[7] "I shake it off, I shake it off (Ooh)"
[8] "I make the moves up as I go (Moves up as I go)"
[9] "I shake it off, I shake it off"
[10] "I, I, I shake it off, I shake it off"
[11] "I, I, I shake it off, I shake it off"
[12] "I, I, I shake it off, I shake it off (Haha)"
[13] "She's like, \"Oh, my God\" (Haha), but I'm just gonna shake"
[14] "We could shake, shake, shake"
[15] "Baby, I'm just gonna shake, shake, shake, shake, shake"
[16] "I shake it off, I shake it off (Ooh; Hey)"
[17] "Heartbreakers gonna break, break, break, break, break (Mmm)"
[18] "And the fakers gonna fake, fake, fake, fake, fake (They fake and fake and fake)"
[19] "Baby, I'm just gonna shake, shake, shake, shake, shake"
[20] "I shake it off, I shake it off (Ooh; Off)"
[21] "I, I, I shake it off, I shake it off"
[22] "I, I, I shake it off, I shake it off (Off)"
[23] "I, I, I shake it off (Yeah), I shake it off"
[24] "I shake it off, I shake it off"
[25] "I, I, I shake it off, I shake it off (You got to)"
[26] "I, I, I shake it off, I shake it off"
[27] "I, I, I shake it off, I shake it off"
# A tibble: 27 × 3
Song Album Lyric
<chr> <chr> <chr>
1 Shake It Off (Taylor's Version) 1989 (Taylor's Version) But I can't make the…
2 Shake It Off (Taylor's Version) 1989 (Taylor's Version) Baby, I'm just gonna…
3 Shake It Off (Taylor's Version) 1989 (Taylor's Version) Shake it off, I shak…
4 Shake It Off (Taylor's Version) 1989 (Taylor's Version) Heartbreakers gonna …
5 Shake It Off (Taylor's Version) 1989 (Taylor's Version) And the fakers gonna…
6 Shake It Off (Taylor's Version) 1989 (Taylor's Version) Baby, I'm just gonna…
7 Shake It Off (Taylor's Version) 1989 (Taylor's Version) I shake it off, I sh…
8 Shake It Off (Taylor's Version) 1989 (Taylor's Version) I make the moves up …
9 Shake It Off (Taylor's Version) 1989 (Taylor's Version) I shake it off, I sh…
10 Shake It Off (Taylor's Version) 1989 (Taylor's Version) I, I, I shake it off…
# ℹ 17 more rows
# A tibble: 22 × 3
Song Album Lyric
<chr> <chr> <chr>
1 Shake It Off (Taylor's Version) 1989 (Taylor's Version) Baby, I'm just gonna…
2 Shake It Off (Taylor's Version) 1989 (Taylor's Version) Shake it off, I shak…
3 Shake It Off (Taylor's Version) 1989 (Taylor's Version) And the fakers gonna…
4 Shake It Off (Taylor's Version) 1989 (Taylor's Version) Baby, I'm just gonna…
5 Shake It Off (Taylor's Version) 1989 (Taylor's Version) I shake it off, I sh…
6 Shake It Off (Taylor's Version) 1989 (Taylor's Version) I shake it off, I sh…
7 Shake It Off (Taylor's Version) 1989 (Taylor's Version) I, I, I shake it off…
8 Shake It Off (Taylor's Version) 1989 (Taylor's Version) I, I, I shake it off…
9 Shake It Off (Taylor's Version) 1989 (Taylor's Version) I, I, I shake it off…
10 Shake It Off (Taylor's Version) 1989 (Taylor's Version) We could shake, shak…
# ℹ 12 more rows
[1] "'" "'" "'" "(" "'" "," "'" "'" "'" "'" "'" "'" "," "," "," "," "," "," ","
[20] "'" "'" "'" "'" "(" "'" "'" "'" "'" "'" "," "," "," "," "," "," "," "'" "-"
[39] "'" "'" "," "," "'" "," "," "," "," "," "," "," "," "," "," "," "," "," ","
[1] "list"
[[1]]
[1] "'"
[[2]]
[1] "'" "," "-"
[[3]]
[1] "'" "," "-"
[[4]]
[1] "(" ")"
[[5]]
[1] "'"
[[6]]
[1] "," "'" "," "-"
[[7]]
[1] "'" "," "-"
[[8]]
[1] "'"
[[9]]
[1] "'" "," "'" "'"
[[10]]
[1] "'"
[[11]]
[1] "'" "," "\"" "'" "\""
[[12]]
[1] "'" "," "," "," ","
[[13]]
[1] "," "," "," ","
[[14]]
[1] "," "'" "," "," "," ","
[[15]]
[1] "," "(" ")"
[[16]]
[1] "," "," "," ","
[[17]]
[1] "," "," "," ","
[[18]]
[1] "," "'" "," "," "," ","
[[19]]
[1] "," "(" ")"
[[20]]
[1] "'" "'"
[[21]]
[1] "'" "'" "," "-"
[[22]]
[1] "'" "'" "," "-"
[[23]]
[1] "'" "'" "(" "'" ")"
[[24]]
[1] "(" ")"
[[25]]
[1] "'" "'" "," "-"
[[26]]
[1] "'" "'" "," "-"
[[27]]
[1] "'"
[[28]]
[1] "'" "," "'" "'"
[[29]]
[1] "'"
[[30]]
[1] ","
[[31]]
[1] "," "," ","
[[32]]
[1] "," "," ","
[[33]]
[1] "," "," "," "(" ")"
[[34]]
[1] "," ","
[[35]]
[1] "," "'" "'"
[[36]]
[1] ","
[[37]]
[1] "'" "'"
[[38]]
[1] "-"
[[39]]
[1] "'" "," "\"" "," "\"" "(" ")" "," "'"
[[40]]
[1] "'" "," "?"
[[41]]
[1] "," ","
[[42]]
[1] "," ","
[[43]]
[1] "'" "," "," "," ","
[[44]]
[1] "," "," "," "," "(" ")"
[[45]]
[1] "," "'" "," "," "," ","
[[46]]
[1] "," "(" ";" ")"
[[47]]
[1] "," "," "," "," "(" ")"
[[48]]
[1] "," "," "," "," "(" ")"
[[49]]
[1] "," "'" "," "," "," ","
[[50]]
[1] "," "(" ";" ")"
[[51]]
[1] "," "," ","
[[52]]
[1] "," "," "," "(" ")"
[[53]]
[1] "," "," "(" ")" ","
[[54]]
[1] ","
[[55]]
[1] "," "," "," "(" ")"
[[56]]
[1] "," "," ","
[[57]]
[1] "," "," ","
[1] │ I stay out too late
[2] │ Got nothin' in my brain
[3] │ That's what people say, mm-mm
[4] │ That's what people say, mm-mm
[5] │ I go on too many dates (Haha)
[6] │ But I can't make them stay
[7] │ At least, that's what people say, mm-mm
[8] │ That's what people say, mm-mm
[9] │ But I keep cruisin'
[10] │ Can't stop, won't stop movin'
[11] │ It's like I got this music in my mind
[12] │ Sayin', "It's gonna <be> alright"
[13] │ 'Cause the players gonna <play>, play, play, play, play
[14] │ And the haters gonna <hate>, hate, hate, hate, hate
[15] │ Baby, I'm just gonna <shake>, shake, shake, shake, shake
[16] │ Shake it off, I shake it off (Ooh)
[17] │ Heartbreakers gonna <break>, break, break, break, break
[18] │ And the fakers gonna <fake>, fake, fake, fake, fake
[19] │ Baby, I'm just gonna <shake>, shake, shake, shake, shake
[20] │ I shake it off, I shake it off (Ooh)
... and 40 more
# A tibble: 14 × 4
Song Album Lyric after
<chr> <chr> <chr> <chr>
1 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "Sayin', \"It'… be
2 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "'Cause the pl… play
3 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "And the hater… hate
4 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "Baby, I'm jus… shake
5 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "Heartbreakers… break
6 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "And the faker… fake
7 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "Baby, I'm jus… shake
8 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "She's like, \… shake
9 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "'Cause the pl… play
10 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "And the hater… hate
11 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "Baby, I'm jus… shake
12 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "Heartbreakers… break
13 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "And the faker… fake
14 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "Baby, I'm jus… shake
[1] "I stay out too late"
[2] "Got nothin' in my brain"
[3] "That's what people say, mm-mm"
[4] "That's what people say, mm-mm"
[5] "I go on too many dates (Haha)"
[6] "But I can't make them stay"
[7] "At least, that's what people say, mm-mm"
[8] "That's what people say, mm-mm"
[9] "But I keep cruisin'"
[10] "Can't stop, won't stop movin'"
[11] "It's like I got this music in my mind"
[12] "Sayin', \"It's gonna be alright\""
[13] "'Cause the players gonna play, play, play, play, play"
[14] "And the haters gonna hate, hate, hate, hate, hate"
[15] "Baby, I'm just gonna bake, shake, shake, shake, shake"
[16] "Shake it off, I bake it off (Ooh)"
[17] "Heartbreakers gonna break, break, break, break, break"
[18] "And the fakers gonna fake, fake, fake, fake, fake"
[19] "Baby, I'm just gonna bake, shake, shake, shake, shake"
[20] "I bake it off, I shake it off (Ooh)"
[21] "I never miss a beat"
[22] "I'm lightnin' on my feet"
[23] "And that's what they don't see, mm-mm"
[24] "That's what they don't see, mm-mm"
[25] "I'm dancin' on my own (Dancin' on my own)"
[26] "I make the moves up as I go (Moves up as I go)"
[27] "And that's what they don't know, mm-mm"
[28] "That's what they don't know, mm-mm"
[29] "But I keep cruisin'"
[30] "Can't stop, won't stop groovin'"
[31] "It's like I got this music in my mind"
[32] "I bake it off, I shake it off"
[33] "I, I, I bake it off, I shake it off"
[34] "I, I, I bake it off, I shake it off"
[35] "I, I, I bake it off, I shake it off (Haha)"
[36] "Hey, hey, hey"
[37] "Just think, while you've been gettin' down and out about the liars"
[38] "And the dirty, dirty cheats of the world"
[39] "You could've been gettin' down to this sick beat"
[40] "My ex-man brought his new girlfriend"
[41] "She's like, \"Oh, my God\" (Haha), but I'm just gonna bake"
[42] "And to the fella over there with the hella good hair"
[43] "Won't you come on over, baby?"
[44] "We could bake, shake, shake"
[45] "Yeah, oh, woah"
[46] "'Cause the players gonna play, play, play, play, play"
[47] "And the haters gonna hate, hate, hate, hate, hate (Haters gonna hate)"
[48] "Baby, I'm just gonna bake, shake, shake, shake, shake"
[49] "I bake it off, I shake it off (Ooh; Hey)"
[50] "Heartbreakers gonna break, break, break, break, break (Mmm)"
[51] "And the fakers gonna fake, fake, fake, fake, fake (They fake and fake and fake)"
[52] "Baby, I'm just gonna bake, shake, shake, shake, shake"
[53] "I bake it off, I shake it off (Ooh; Off)"
[54] "I, I, I bake it off, I shake it off"
[55] "I, I, I bake it off, I shake it off (Off)"
[56] "I, I, I bake it off (Yeah), I shake it off"
[57] "I bake it off, I shake it off"
[58] "I, I, I bake it off, I shake it off (You got to)"
[59] "I, I, I bake it off, I shake it off"
[60] "I, I, I bake it off, I shake it off"
[1] "I stay out too late"
[2] "Got nothin' in my brain"
[3] "That's what people say, mm-mm"
[4] "That's what people say, mm-mm"
[5] "I go on too many dates (Haha)"
[6] "But I can't make them stay"
[7] "At least, that's what people say, mm-mm"
[8] "That's what people say, mm-mm"
[9] "But I keep cruisin'"
[10] "Can't stop, won't stop movin'"
[11] "It's like I got this music in my mind"
[12] "Sayin', \"It's gonna be alright\""
[13] "'Cause the players gonna play, play, play, play, play"
[14] "And the haters gonna hate, hate, hate, hate, hate"
[15] "Baby, I'm just gonna bake, bake, bake, bake, bake"
[16] "Shake it off, I bake it off (Ooh)"
[17] "Heartbreakers gonna break, break, break, break, break"
[18] "And the fakers gonna fake, fake, fake, fake, fake"
[19] "Baby, I'm just gonna bake, bake, bake, bake, bake"
[20] "I bake it off, I bake it off (Ooh)"
[21] "I never miss a beat"
[22] "I'm lightnin' on my feet"
[23] "And that's what they don't see, mm-mm"
[24] "That's what they don't see, mm-mm"
[25] "I'm dancin' on my own (Dancin' on my own)"
[26] "I make the moves up as I go (Moves up as I go)"
[27] "And that's what they don't know, mm-mm"
[28] "That's what they don't know, mm-mm"
[29] "But I keep cruisin'"
[30] "Can't stop, won't stop groovin'"
[31] "It's like I got this music in my mind"
[32] "I bake it off, I bake it off"
[33] "I, I, I bake it off, I bake it off"
[34] "I, I, I bake it off, I bake it off"
[35] "I, I, I bake it off, I bake it off (Haha)"
[36] "Hey, hey, hey"
[37] "Just think, while you've been gettin' down and out about the liars"
[38] "And the dirty, dirty cheats of the world"
[39] "You could've been gettin' down to this sick beat"
[40] "My ex-man brought his new girlfriend"
[41] "She's like, \"Oh, my God\" (Haha), but I'm just gonna bake"
[42] "And to the fella over there with the hella good hair"
[43] "Won't you come on over, baby?"
[44] "We could bake, bake, bake"
[45] "Yeah, oh, woah"
[46] "'Cause the players gonna play, play, play, play, play"
[47] "And the haters gonna hate, hate, hate, hate, hate (Haters gonna hate)"
[48] "Baby, I'm just gonna bake, bake, bake, bake, bake"
[49] "I bake it off, I bake it off (Ooh; Hey)"
[50] "Heartbreakers gonna break, break, break, break, break (Mmm)"
[51] "And the fakers gonna fake, fake, fake, fake, fake (They fake and fake and fake)"
[52] "Baby, I'm just gonna bake, bake, bake, bake, bake"
[53] "I bake it off, I bake it off (Ooh; Off)"
[54] "I, I, I bake it off, I bake it off"
[55] "I, I, I bake it off, I bake it off (Off)"
[56] "I, I, I bake it off (Yeah), I bake it off"
[57] "I bake it off, I bake it off"
[58] "I, I, I bake it off, I bake it off (You got to)"
[59] "I, I, I bake it off, I bake it off"
[60] "I, I, I bake it off, I bake it off"
# A tibble: 21 × 3
Song Album Lyric
<chr> <chr> <chr>
1 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "Baby, I'm just gonn…
2 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "Shake it off, I run…
3 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "Baby, I'm just gonn…
4 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "I run it off, I run…
5 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "I run it off, I run…
6 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "I, I, I run it off,…
7 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "I, I, I run it off,…
8 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "I, I, I run it off,…
9 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "She's like, \"Oh, m…
10 Shake It Off (Taylor's Version) 1989 (Taylor's Version) "We could run, run, …
# ℹ 11 more rows