Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove information using gsub

Tags:

string

regex

r

I am trying to remove all the information up to the last / (the year) from this line.

"'CSF0495/DE/wb/1997'"

The code I run goes through but does not remove any information.

This it the code I run

date <- gsub("^[[:alnum:]]{1,}////", "",temp)

It goes through, but does not remove any information. Im not sure what I am missing.

like image 438
sk1542 Avatar asked Dec 12 '25 11:12

sk1542


2 Answers

Using sub

sub(".*/", "", "CSF0495/DE/wb/1997")
[1] "1997"

Or with basename

basename("CSF0495/DE/wb/1997")
[1] "1997"
like image 184
akrun Avatar answered Dec 15 '25 17:12

akrun


Why not simply extract the year, knowing that it consists of more than one digits extending as far as final position in string ($)?

library(stringr)
str_extract("CSF0495/DE/wb/1997", "\\d+$")
[1] "1997"
like image 30
Chris Ruehlemann Avatar answered Dec 15 '25 17:12

Chris Ruehlemann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!