0

I have a string named rids.csf. I want to extract csf from it. How I can do it. I tried the following code but it did not work for me.

string<-"rids.csf"
sub(".","", string)
## outcome I got
"ids.csf"
## expected outcome
"csf"
jeetkamal
  • 399
  • 2
  • 12

1 Answers1

1

We can match characters (.*) till the . (escaped as . is a metacharacter) and replaced with blank (""_

sub(".*\\.", "", string)
#[1] "csf"

. in regex mode (by default it is fixed = FALSE) matches any character and because of that it matches the first character 'r' and removed it

akrun
  • 874,273
  • 37
  • 540
  • 662