Skip to content
Back to formatted view

Raw Message

Message-ID: <OF5671FBBD.BB352E12-ON802574D4.00546EF2-802574D4.0054D0A5@hsl.gov.uk>
Date: 2008-09-30T15:26:24Z
From: Richard Cotton
Subject: Pattern match in R
In-Reply-To: <19743389.post@hsl.gov.uk>

> I want to make sure this piece of code I wrote is doing what I want it 
to do.
> 
> ll<-function(string)
> {
>    grep(string,dir(),value=T)
> }
> 
> 
> subdir = ll("Coverage_[1-9][0-9]$")
> 
> I basically wrote a little function that would grab all the files of 
form
> Coverage_[0-99]
> 
> The way I wrote it, will it grab Coverage_5 or does it have to have 2
> numbers (10-99)?

This is straightforward to test.
filenames <- paste("Coverage", 0:99, sep="_")
grep("Coverage_[1-9][0-9]$", filenames, value=TRUE)

This returns:
[1] "Coverage_10" "Coverage_11" "Coverage_12" "Coverage_13"
 [5] "Coverage_14" "Coverage_15" "Coverage_16" "Coverage_17"
...
[89] "Coverage_98" "Coverage_99"

If you want all the files (Coverage_0 through to coverage_99), try this 
instead:
grep("Coverage_[0-9]{1,2}$", filenames, value=TRUE)

You may wish to take a look at the Repetition section on this page:
http://www.regular-expressions.info/quickstart.html

Regards,
Richie.

Mathematical Sciences Unit
HSL


------------------------------------------------------------------------
ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}