Tyler Rinker
Tyler Rinker

Reputation: 109844

regex from first character to the end of the string

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

Upvotes: 3

Views: 1890

Answers (3)

CHP
CHP

Reputation: 17189

Try below

dob [1] "9/9/43 12:00 AM/PM" "9/17/88 12:00 AM/PM"
[3] "11/21/48 12:00 AM/PM"
gsub("(.?) (.$)", "\2", dob)
[1] "12:00 AM/PM" "12:00 AM/PM" "12:00 AM/PM"

Upvotes: 1

juba
juba

Reputation: 49033

Try :

gsub("^(.*?) .*$", "\\1", dob)
# [1] "9/9/43"   "9/17/88"  "11/21/48"

If you want from the first space to the end of the string, try :

gsub("^.*? (.*)$", "\\1", dob)
# [1] "12:00 AM/PM" "12:00 AM/PM" "12:00 AM/PM"

Upvotes: 4

Daniel R.
Daniel R.

Reputation: 66

You forgot the indicator for the beginning of the string:

gsub("^.*? ", "", dob)

Note the caret at the beginning. Your first solution wasn't too greedy, but found two strings and replaced them.

Upvotes: 2

Related Questions