[R] Regular Expressions {was Processing all *.dat worked..}

Martin Maechler maechler at stat.math.ethz.ch
Tue Aug 14 08:48:48 CEST 2001


>>>>> "PaulEJ" == Paul E Johnson <pauljohn at ukans.edu> writes:

    PaulEJ> I got great, quick advice here earlier today.  One little hitch
    PaulEJ> was that the pattern in list.files wants a regular expression,
    PaulEJ> and *.dat was grabbing all files with .dat in them, not ones
    PaulEJ> ending in .dat.  This code creates a *.dat.summary file with
    PaulEJ> the mean and std. deviation. of the numeric variables for every
    PaulEJ> *.dat file in the current directory.

    PaulEJ> myDat<-list.files(pattern="*.dat$")

(many of us would say  ``please use spaces around "<-" ''
 [recent versions of ESS do this automagically with the "_" key!])

    PaulEJ>  <.....>

You are right: Pattern should be a  *regular expression*

However, it was ``pure luck'' that your  "*.dat$"  worked fine.
Both "*" and "." don't mean what you think they mean;

  "."  means  ``any arbitrary character''
  "*"  means  ``the previous [thing] arbitrary many (0,1,...) times''
	      where [thing] is a single character (or something more general
						   for advanced usage)

The correct pattern would have been

    pattern = "\\.dat$"

which really is \.dat$ 
{ the "\\" (doubling) is necessary to specify a single "\" in an R string}

You can find a bit more about regular expression using R's help
  ?grep    and
  ?apropos

Regular Expressions (nicknamed "Regexp"s, "Regex"s or "RE"s) are a very
powerful concept from the early years of Unix; probably first used for the
"grep" (and "egrep") command; then, inside Emacs and lately Perl, Python,
etc.  All regexp implementations have quite a few things in common,
but slightly differ when it becomes complicated.  
It's worth spending an hour or so learning the basics.
A google search gives you tons of starting points.


Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO D10	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list