Denny Chen
Denny Chen

Reputation: 499

Remove multiple slashes at the end of a string and leave one only in R

For my case, it's an web strings, but here I will just give an easier example:`

df = data.frame(Strings = c("abc/d/e/f////", "abc///", "/", "a/bc/d/////"))

The result that I want to gain is make the string which is ended with multiple "/" into there is only one "/" left at the end of the strings.

Which means that for the df I gave above, I want to get a result like this:

df_result = data.frame(Strings = c("abc/d/e/f/", "abc/", "/", "a/bc/d/"))

Thanks for answering my question.

Upvotes: 0

Views: 66

Answers (1)

NelsonGon
NelsonGon

Reputation: 13319

We could use:

string1<-"abc/d/e/f///"
gsub("/(?=/{1,})","",string1,perl=TRUE)
#[1] "abc/d/e/f/"

Upvotes: 1

Related Questions