I am trying to get rid of all the numbers/characters coming in AFTER the FIRST hyphen.
here are some examples:
gsub(pattern = '[0-9]*-$', replacement = "", x = data$id)
grep(pattern = '[0-9]*-', replacement = "", x = data$id)
regexpr(pattern = '[0-9]*-', text = data$id)
Several ways to achieve this, here is one:
have <- c("15-103025-01", "800-40170-02", "68-4974-01") want <- sub(pattern = "(^\\d+\\-).*", replacement = "\\1", x = have)
So in your regular expression, you'll have one group created with
()'s, which matches the start of the string (
^) followed by one or more numbers (
\\d+) and the hyphen (
\\-). Outside the group is any other character(s) that follow (
In the replacement part, you specify
\\1 to refer to the first (and only) group of the regular expression. Not adding anything else means dropping all the rest.