More regular expressions

String Manipulation with stringr in R

Charlotte Wickham

Assistant Professor at Oregon State University

Regular expression review

Pattern Regular expression rebus
Start of string ^ START
End of string $ END
Any single character . ANY_CHAR
String Manipulation with stringr in R

Regular expression review

Pattern Regular expression rebus
Start of string ^ START
End of string $ END
Any single character . ANY_CHAR
String Manipulation with stringr in R

Regular expression review

Pattern Regular expression rebus
Start of string ^ START
End of string $ END
Any single character . ANY_CHAR
String Manipulation with stringr in R

Regular expression review

Pattern Regular expression rebus
Start of string ^ START
End of string $ END
Any single character . ANY_CHAR
String Manipulation with stringr in R

Regular expression review

Pattern Regular expression rebus
Start of string ^ START
End of string $ END
Any single character . ANY_CHAR
String Manipulation with stringr in R

Regular expression review

Pattern Regular expression rebus
Start of string ^ START
End of string $ END
Any single character . ANY_CHAR
Literal dot, carat, dollar \., \^, \$ DOT, CARAT, DOLLAR
String Manipulation with stringr in R

Alternation

(dog|cat)

or("dog", "cat")
<regex> (?:dog|cat)`
str_view(c("kittycat", "doggone"), 
    pattern = or("dog", "cat"))

chap_3_2.010.png

String Manipulation with stringr in R

Character classes

char_class("Aa")
<regex> [Aa]
str_view(c("apple", "Aaron"), 
  pattern = char_class("Aa"))
negated_char_class("Aa")
<regex> [^Aa]
str_view(c("apple", "Aaron"), 
  pattern = negated_char_class("Aa"))

  3-2-12-1.png

    3-2-12-2.png

String Manipulation with stringr in R

Repetition

Pattern Regular expression rebus
Optional ? optional()
Zero or more * zero_or_more()
One or more + one_or_more()
Between m and n times {m,n} repeated()
String Manipulation with stringr in R

Repetition

str_view(c("apple", "Aaron"), 
  pattern = one_or_more("Aa"))

chap_3_2.016.png

String Manipulation with stringr in R

Let's practice!

String Manipulation with stringr in R

Preparing Video For Download...