[1] FALSE TRUE FALSE FALSE FALSE
Lecture 13
College of Idaho
CSCI 2025 - Winter 2026
stringr provides a consistent interface for working with regex.str_detect()str_detect() returns TRUE if a pattern is found in a string, FALSE otherwise.str_count()str_count() counts the number of matches in a string.str_extract() and str_extract_all()str_extract() extracts the first match.str_extract_all() extracts all matches.str_replace() and str_replace_all()Instead of inputting literal strings, you can use regex patterns to describe more complex matches.
^ matches the start of the string.$ matches the end of the string.. matches any character except a newline.\d matches any digit.\s matches any whitespace.[abc] matches a, b, or c.[^abc] matches anything except a, b, or c.| matches either the expression before or after the |.?: 0 or 1 time.+: 1 or more times.*: 0 or more times.{n}: exactly n times.{n,}: n or more times.{n,m}: between n and m times.What do each of these do?
"^b.*a$""^.{5}$""[aeiou]"() for Grouping\1, \2, etc. refer to previously captured groups.Exercise 6 from 15.4.7.
tidyr::separate_wider_regex()fixed()fixed() to match a literal string without interpreting it as a regex.Let’s extract the AR codes and the county names from the Naturalization data!