[R] ? Exact pattern matching in GREP ?

Jeff Gentry jgentry at jimmy.harvard.edu
Fri Sep 27 17:15:07 CEST 2002


> # 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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list