Reputation: 10697
I have the string foo+1234+bar.txt
, and I'm trying to substitute foo
with baz
and remove +bar
:
baz+1234.txt
I think I should use gsub
gsub("foo*", "baz", "foo+1234+bar.txt")
but I don't know how to selectively keep some of the characters while removing others at the same time.
Upvotes: 0
Views: 63
Reputation: 388982
You can do this as chained operation. First remove '+bar'
then replace 'foo'
with 'baz'
.
string <- "foo+1234+bar.txt"
sub('foo', 'baz', fixed = TRUE, sub('+bar', '', string, fixed = TRUE))
#[1] "baz+1234.txt"
Upvotes: 1