How is exact pattern matching achieved in GREP (and GREPlike) functions ? # Want: listing of all object names that end in *.lm> objects(pattern="*.lm",pos=1)# ... but get: all objects that partially match *.lm, e.g., *.lme [1] "j3.lm" "J3.lme" "j8.lm" "J8.lme" # Want: position of string "4jan2002" in vector> date.index <- grep("4jan2002", my.dates)# .... but get:> my.dates[date.index][1] "4jan2002" "24jan2002" "14jan2002" Thank you. Derek N. Eder G?teborgs Universitet Institutionen f?r klinisk neurovetenskap Klinisk Neurofysiologi Sahlgrenska universitetssjukhuset SS/SU Bl? straket 7, v?n 3 SE 413 45 G?teborg Sverige Tlf. +46 (031) 34 244 14 (office) NYT! Tlf. +46 (031) 34 212 83 (laboratory) Tlf. +46 0709 / 7 212 83 (mobil) Fax. +46 (031) 82 81 63 derek.eder at neuro.gu.se Gothenburg University Institute of Clinical Neuroscience, Department of Clinical Neurophysiology Salhgrenska Hospital SU/SS SE 413 45 G?teborg Sweden -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> How is exact pattern matching achieved in GREP (and GREPlike) > functions ?From ?grep: The regular expressions used are those specified by POSIX 1003.2, either extended or basic, depending on the value of the `extended' argument. On how to specify the expressions, see "man re_format" or Google for "POSIX 1003.2".> # Want: listing of all object names that end in *.lm >> objects(pattern="*.lm",pos=1)> grep("\\.lm$",c("test1.lm","test.lm.txt","testlm")) [1] 1> # Want: position of string "4jan2002" in vector >> date.index <- grep("4jan2002", my.dates) > # .... but get: >> my.dates[date.index] > [1] "4jan2002" "24jan2002" "14jan2002"> grep("^4jan2002$",c("4jan2002","24jan2002","14jan2002b","4jan2002b")) [1] 1 - Hedderik. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> # Want: listing of all object names that end in *.lm > > objects(pattern="*.lm",pos=1) > # ... but get: all objects that partially match *.lm, e.g., *.lme > [1] "j3.lm" "J3.lme" "j8.lm" "J8.lme"Use "$" at the end of your pattern to denote that this should be the exact end of the string. As help("grep") explains, the regexps are POSIX based. On my local system, a 'man grep' (ie the unix command, not the R command) provides a section on the particulars for such regexps.> a <- c("j3.lm","J3.lme","j8.lm","J8.lme") > a[1] "j3.lm" "J3.lme" "j8.lm" "J8.lme"> a[grep(".lm$",a)][1] "j3.lm" "j8.lm"> # Want: position of string "4jan2002" in vector > # .... but get: > > my.dates[date.index] > [1] "4jan2002" "24jan2002" "14jan2002"Here you want to use '^' at the start of the pattern to note that this is the *start* of the string. (Much like the '$' before):> a <- c("4jan2002","24jan2002","14jan2002") > a[1] "4jan2002" "24jan2002" "14jan2002"> a[grep("^4jan2002",a)][1] "4jan2002" -J -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Fri, 27 Sep 2002, Derek Eder wrote:> How is exact pattern matching achieved in GREP (and GREPlike) functions ?By choosing the right pattern :) Regular expressions are designed to match substrings of a string, and can do very complicated matching. They have a different syntax from filename wildcard expressions like *.lm, and don't automatically tie down the beginning or end of a string. Google for 'regular expressions' to find some helpful descriptions.> # Want: listing of all object names that end in *.lm > > objects(pattern="*.lm",pos=1) > # ... but get: all objects that partially match *.lm, e.g., *.lme > [1] "j3.lm" "J3.lme" "j8.lm" "J8.lme"The regular expression *.lm matches any string containing at least one character followed by the "lm", so it matches all of your examples and would also match "almost" and "calmly" but would not match, for example, "lm" or "lme". You appear to want a string ending in ".lm", for which the pattern is "\\.lm$". The "\\." matches a "." (where a "." pattern matches any character), and the "$" matches the end of line.> > # Want: position of string "4jan2002" in vector > > date.index <- grep("4jan2002", my.dates) > # .... but get: > > my.dates[date.index] > [1] "4jan2002" "24jan2002" "14jan2002" >If you want exact string equality the best approach is to use == rather than grep, eg which(my.dates == "4jan2002") It's possible to do this using grep(), but it's harder -- you are looking for a string that starts "4jan2002" and then ends. The regular expression for this is "^4jan2002$" Filename wildcards translate like this: *.lm "\\.lm$" test.* "^test\\." test?.lm "^test.\\.lm$" test.lm* "^test\\.lm" *.* "\\." -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Seemingly Similar Threads
- Running R away from windows (to Linux)
- Scope problems - passing an argument from one function to another (LME) within
- Regular Expressionsionism
- Fwd: Re: Combining many dataframes from listings of objects?
- Returning position of mouse cursor (without button press) ?