[R] Regular Expressions in grep

arun smartpink111 at yahoo.com
Wed Aug 22 02:05:35 CEST 2012


HI,
Try this:
gsub("^-\\d(\\d{4}.).*","\\1",a)
#[1] "1020."
gsub("^.*(.\\d{5}).","\\1",a)
#[1] ".90920"
A.K.



----- Original Message -----
From: Dr. Holger van Lishaut <H.v.Lishaut at gmx.de>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Tuesday, August 21, 2012 3:24 PM
Subject: [R]  Regular Expressions in grep

Dear r-help members,

I have a number in the form of a string, say:

a<-"-01020.909200"

I'd like to extract "1020." as well as ".9092"

Front<-grep(pattern="[1-9]+[0-9]*\\.", value=TRUE, x=a, fixed=FALSE)
End<-grep(pattern="\\.[0-9]*[1-9]+", value=TRUE, x=a, fixed=FALSE)

However, both strings give "-01020.909200", exactly a.
Could you please point me to what is wrong?

Thanks and best regards
H. van Lishaut

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list