In a related post someone asked how to grab from beginning of string to first occurrence of a character. I'd like to extend my own knowledge of regex by asking how to grab from a certain character of the string to the end.
How could I use regex (not strsplit
) with gsub to grab from the beginning of the first space to the end of the string?
dob <- c("9/9/43 12:00 AM/PM", "9/17/88 12:00 AM/PM", "11/21/48 12:00 AM/PM")
Here I tried: gsub(".*? ", "", dob)
but it grabs from the last space not the first so I tried gsub(".{1}? ", "", dob)
but it is overly greedy because of the period.
Final solution would be the same as:
sapply(lapply(strsplit(dob, "\\s+"), "[", 2:3), paste, collapse=" ")
##[1] "12:00 AM/PM" "12:00 AM/PM" "12:00 AM/PM"
NOTE: R regex is not identical to regex in general