Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do complex regular expressions in list.files in R

Tags:

regex

r

I cannot find any example on how to use the pattern= parameter in list.files for more complex operations.

I would like to get all the files that include either 'XM' or 'EM' and are of the .cvs type

Can someone please help me.

I was trying something like

list.files(path='.', pattern="[XM | EM] & csv")

but it is definitely wrong

like image 888
pedrosaurio Avatar asked Jan 26 '12 16:01

pedrosaurio


People also ask

What is difference [] and () in regex?

In other words, square brackets match exactly one character. (a-z0-9) will match two characters, the first is one of abcdefghijklmnopqrstuvwxyz , the second is one of 0123456789 , just as if the parenthesis weren't there. The () will allow you to read exactly which characters were matched.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string).

How do you make a regular expression in R?

To create that regular expression, you need to use a string, which also needs to escape \ . That means to match a literal \ you need to write "\\\\" — you need four backslashes to match one! In this book, I'll write regular expression as \. and strings that represent the regular expression as "\\." .

What type of regex does r use?

Two types of regular expressions are used in R, extended regular expressions (the default) and Perl-like regular expressions used by perl = TRUE . There is also fixed = TRUE which can be considered to use a literal regular expression.


Video Answer


1 Answers

try this:

list.files(path='.', pattern="(XM|EM).*\\.csv$")
like image 180
Tom Avatar answered Oct 19 '22 13:10

Tom