I have a number of files in my folder, e.g.:
I only wish R to find "file1.txt". But if I use
list.files(pattern = "file1.txt")
R will also return the other two files from my example. Any ideas how I could solve this?
Thanks!
Use regular expressions (see ?regex
):
list.files(pattern = "^file1\\.txt$")
^
is the regular expression denoting the beginning of the string,\\
escapes the .
to make it a literal .
,$
is the regular expression denoting the end of the string.
In sum, this is the regular expression capturing exactly file1.txt
and nothing else.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With