Regex to remove .csv in r

2.2k views Asked by At

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)

4

There are 4 answers

2
akrun On BEST ANSWER

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"    
4
user227710 On
word <- c("dirtyboards.csv","boardcsv.csv")
sub(".csv$","",word)
[1] "dirtyboards" "boardcsv"   
3
Pierre L On

If your situation is like it is presented, you can try:

substr(word, 1, nchar(word)-4)
[1] "dirtyboards"

This code starts from the first character and captures the rest of the string up to the last four tokens. The solution wholly depends on the application involved.

1
Tyler Rinker On

Base R has an ap for that:

word <- "dirtyboards.csv" 
tools::file_path_sans_ext(word)

## [1] "dirtyboards"