Regular expressions

String Manipulation with stringr in R

Charlotte Wickham

Assistant Professor at Oregon State University

Regular expressions

  • A language for describing patterns

chap_3_1.004.png

  • "the start of the string, followed by any single character, followed by one or more digits"
String Manipulation with stringr in R

Regular expressions as a pattern argument

str_detect(c("R2-D2", "C-3P0"), pattern = "^.\\d+")
TRUE FALSE
START %R% 
    ANY_CHAR %R% 
    one_or_more(DGT)
<regex> ^.[\d]+

chap_3_1.018.png

String Manipulation with stringr in R

Regular expressions as a pattern argument

str_detect(c("R2-D2", "C-3P0"), 
  pattern = START %R% 
            ANY_CHAR %R% 
            one_or_more(DGT)) 
TRUE FALSE
str_view(c("R2-D2", "C-3P0"), 
           pattern = START %R% 
                     ANY_CHAR %R% 
                     one_or_more(DGT))

chap_3_1.022.png

String Manipulation with stringr in R

Let's practice!

String Manipulation with stringr in R

Preparing Video For Download...