Asda
Asda

Reputation: 1655

gsub: Removing a special character at the end of a string in R

I am looking to replace all *s at the end of a string, in R.

txt <- c("arm","foot-ha-*","lefroo", "bafoobar*")
gsub("/\\*$/","",txt, perl=TRUE); 

I would like to convert txt to be "arm" "foo-ha-" "lefroo" "bafoobar"

What am I doing wrong?

Thanks!

Upvotes: 2

Views: 8904

Answers (1)

Hong Ooi
Hong Ooi

Reputation: 57697

You don't need the /s in the regex.

gsub("\\*$", "", txt)

Upvotes: 4

Related Questions