Regular Expressions: Intro

(Sections 12.1 - 12.2)

Motivation

Packages

Make sure these are attached:

library(bcscr)
library(tidyverse)

Some Data

Just a few names and phone numbers:

?NamePhone
View(NamePhone)

What We Have

What We Want

How to Get It:

desired <-
  NamePhone %>% 
  tidyr::extract(
    col = name,
    into = c("last", "first"),
    regex = "(\\w+), (\\w+)"
  ) %>% 
  tidyr::extract(
    col = phone,
    into = c("area", "office", "line"),
    regex = "(\\d{3})\\D*(\\d{3})\\D*(\\d{4})"
  )

Black Magic?

(\\w+), (\\w+)

(\\d{3})\\D*(\\d{3})\\D*(\\d{4})

These are regular expressions.

Learn and Practice

Regex Sites

Our go-to site will be this one:

https://regexr.com/

(Switch from JavaScript to PCRE.)

You might also enjoy:

https://regex101.com/