Hi,
On Aug 26, 2009, at 6:38 PM, Martin Batholdy wrote:
hi,
is there an easy way to extract numbers from a string?
for example I have;
"this Item costs 3.32 Dollars"
is there an easy way to extract the 3.32 as a number?
Regular expressions to the rescue?
Perhaps you'll need to fine tune it, but see here:
R> gregexpr("(\\d+(\\.\\d+)?)", "this Item costs 3.32 Dollars", perl=T)
[[1]]
[1] 17
attr(,"match.length")
[1] 4
R> gregexpr("(\\d+(\\.\\d+)?)", "this Item costs 3.32 Dollars, that
item costs 10.12 dollars", perl=T)
[[1]]
[1] 17 47
attr(,"match.length")
[1] 4 5
R> gregexpr("(\\d+(\\.\\d+)?)", "this Item costs 3.32 Dollars, that
item costs 10 dollars even, ", perl=T)
[[1]]
[1] 17 47
attr(,"match.length")
[1] 4 2
R> gregexpr("(\\d+(\\.\\d+)?)", "this one is free ", perl=T)
[[1]]
[1] -1
attr(,"match.length")
[1] -1
HTH,
-steve
--
Steve Lianoglou
Graduate Student: Computational Systems Biology
| Memorial Sloan-Kettering Cancer Center
| Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact
______________________________________________
R-help@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.