Tiago
Tiago

Reputation: 153

Using pattern to select files that has XX at any part the name in R

I have a folder full of files which names are like these, for example:

[1] "./final_model_pre/pre_pe_ja_bc_wm.tif"          "./final_model_pre/pre_pe_ja_bc_wm.tif.aux.xml" 
 [3] "./final_model_pre/pre_pe_ja_bc_wm.tif.ovr"      "./final_model_pre/pre_an_le_glm_wm.tif"        
 [5] "./final_model_pre/pre_an_le_glm_wm.tif.aux.xml" "./final_model_pre/pre_an_le_glm_wm.tif.ovr"    
 [7] "./final_model_pre/pre_an_bo_ma_wm.tif"          "./final_model_pre/pre_an_bo_ma_wm.tif.aux.xml" 
 [9] "./final_model_pre/pre_an_bo_ma_wm.tif.ovr"      "./final_model_pre/pre_pe_ja_mx_wm.tif"         
[11] "./final_model_pre/pre_pe_ja_mx_wm.tif.aux.xml"  "./final_model_pre/pre_pe_ja_mx_wm.tif.ovr"     
[13] "./final_model_pre/pre_pe_ja_rf1_wm.tif"         "./final_model_pre/pre_pe_ja_rf1_wm.tif.aux.xml"
[15] "./final_model_pre/pre_pe_ja_rf1_wm.tif.ovr"     "./final_model_pre/pre_pe_ja_svm_wm.tif"        
[17] "./final_model_pre/pre_pe_ja_svm_wm.tif.aux.xml" "./final_model_pre/pre_pe_ja_svm_wm.tif.ovr" 

I want to list every files that have "pe_ja" in the name and only with ".tif" extension, not ".tif.ovr" or ".tif.aux.xml" or any other extension. I'm trying to use list.files function, but I couldn't manage to use the pattern agrument properly. Could you help me doing that? Thank you! P.S.: I'm using R.

Upvotes: 0

Views: 744

Answers (2)

Nader Hisham
Nader Hisham

Reputation: 5414

try this one files = list.files(pattern = '.*pe_ja.*\.tif$') note that in order to escape the . you need this expression \. not \\. , this expression \\. means escape \ character and match any character after . except new line try this one out Regex101 test file names

Upvotes: 1

Max Candocia
Max Candocia

Reputation: 4385

You can use a regular expression for that.

files = list.files(pattern = '.*pe_ja.*\\.tif$')

The $ at the end of the regular expression indicates that that is the end of the string. The \\. is an escaped period, indicating that you want to match a period (not any character, which is what . normally matches).

The .* selects any character any number of times (including 0).

Upvotes: 6

Related Questions