As shown in the stringr package, this can be achieved using str_match() or str_extract() .
Adapted from the manual:
library(stringr) strings <- c(" 219 733 8965", "329-293-8753 ", "banana", "239 923 8115 and 842 566 4692", "Work: 579-499-7527", "$1000", "Home: 543.355.3679") phone <- "([2-9][0-9]{2})[- .]([0-9]{3})[- .]([0-9]{4})"
Extracting and combining our groups:
str_extract(strings, phone) # [1] "219 733 8965" "329-293-8753" NA "239 923 8115" "579-499-7527" NA # [7] "543.355.3679"
Specifying groups with an output matrix (we are interested in columns 2 +):
str_match(strings, phone) # [,1] [,2] [,3] [,4] # [1,] "219 733 8965" "219" "733" "8965" # [2,] "329-293-8753" "329" "293" "8753" # [3,] NA NA NA NA # [4,] "239 923 8115" "239" "923" "8115" # [5,] "579-499-7527" "579" "499" "7527" # [6,] NA NA NA NA # [7,] "543.355.3679" "543" "355" "3679"
Megatron Dec 23 '15 at 15:37 2015-12-23 15:37
source share