How to extract substring from a string up to a certain occurrence of a character.
For example:
string <- 'test_file_csv_name.csv'
Up to the second occurrence of _
Expected output:
'test_file'
Using sub
, we can capture the portion of the file name you want, removing the rest:
string <- 'test_file_csv_name.csv'
sub("^([^_]+_[^_]+).*$", "\\1", string)
[1] "test_file"
are you asking to keep a certain amout of _
you can use
sub("((.*?_){1}.*?)_.*","\\1",string)
change {1}
to keep the amount of _
you'd like
Beside using sub
(e.g. sub("([^_]*_[^_]*).*", "\\1", string)
) you can use substr
using the position found by gregexpr
:
substr(string, 1, gregexpr("_", string)[[1]][2]-1)
#[1] "test_file"