Remi.b
Remi.b

Reputation: 18239

Regular expression to find files that match a pattern in R

I am experiencing some difficulties with Regular expression in R.

I am looking for all files that match the following pattern:

the files name should start with "11" and ends with ".JPG"

What regular expression should I use?

list.files(path='my_path', pattern=???)

Thank you

Upvotes: 0

Views: 227

Answers (1)

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193687

You can use ^ to indicate "at the start", $ to indicate "at the end", and .* for all the stuff in between, so you can try something like:

list.files(path='my_path', pattern="^11.*\\.JPG$")

Try this little experiment out and see how the results of each pattern differs:

someFiles <- c("testpost.html", "mytest.html", "testing.html", "testing.txt")
grep("test", someFiles)
grep("^test", someFiles)
grep("\\.txt", someFiles)
grep("^test.*\\.html", someFiles)

Upvotes: 2

Related Questions