lroha
lroha

Reputation: 34441

Remove character from string ignoring first match using regex

How can all instances of a character be removed from a string except for the first match using regex? In the example data below I'd like to keep the first '-' and remove all other instances.

myvec <- c("12-34-5678-9", "ABC-DEF-GHI-JKL-MN", "9-8765", "a - defgh -- ijkl")

The outcome sought is:

"12-3456789"   "ABC-DEFGHIJKLMN"   "9-8765"   "a - defgh  ijkl"

I worked out a solution using strsplit and rebuilt the vectors using paste but I'm looking for a regex approach to satisfy my curiosity.

Upvotes: 1

Views: 381

Answers (2)

akrun
akrun

Reputation: 887118

Try this

 gsub('^[^-]+-(*SKIP)(*F)|-', '', myvec, perl=TRUE)
 #[1] "12-3456789"      "ABC-DEFGHIJKLMN" "9-8765"          "a - defgh  ijkl"

Upvotes: 3

Avinash Raj
Avinash Raj

Reputation: 174706

You may use capturing group.

> myvec <- c("12-34-5678-9", "ABC-DEF-GHI-JKL-MN", "9-8765", "a - defgh -- ijkl")
> gsub("^([^-]*-)|-", "\\1", myvec)
[1] "12-3456789"      "ABC-DEFGHIJKLMN" "9-8765"         
[4] "a - defgh  ijkl"

Upvotes: 2

Related Questions