-2

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'
MGJ-123
  • 524
  • 1
  • 10

3 Answers3

2

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"
Tim Biegeleisen
  • 387,723
  • 20
  • 200
  • 263
1

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

Daniel O
  • 4,143
  • 4
  • 20
1

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"
GKi
  • 20,626
  • 1
  • 11
  • 24