Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to remove .csv in r

Tags:

regex

r

stringr

this is going to be silly.

I have a string like:

word <- "dirtyboards.csv" 

I want to remove the csv part and get "dirtyboards".

I am trying:

require(stringr)
str_extract(word, ".*[^.csv]")

I get in return: "dirtyboard" . The "s" before the ".csv" goes missing.

I know I can do ,

gsub(".csv", "", word)

like image 928
vagabond Avatar asked Dec 17 '25 17:12

vagabond


1 Answers

Try

library(stringr)
str_extract(word, '.*(?=\\.csv)')
#[1] "dirtyboards"

Another option which works for the example provided (and not very specific)

str_extract(word, '^[^.]+')
#[1] "dirtyboards" 

Update

Including 'foo.csv.csv',

word1 <- c("dirtyboards.csv" , "boardcsv.csv", "foo.csv.csv")
str_extract(word1, '.*(?=\\.csv$)')
#[1] "dirtyboards" "boardcsv"    "foo.csv"    
like image 83
akrun Avatar answered Dec 20 '25 08:12

akrun



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!