[R] extracting values from txt with regular expression

emorway emorway at usgs.gov
Thu Jun 7 19:40:54 CEST 2012


Thanks for your suggestions.  Bert, in your response you raised my awareness
to "regular expressions".  Are regular expressions the same across various
languages?  Consider the following line of text:

txt_line<-" PERCENT DISCREPANCY =           0.01     PERCENT DISCREPANCY =         
-0.05"

It seems python uses the following line of code to extract the two values in
"txt_line" and store them in a variable called "v":

v = re.findall("[+-]? *(?:\d+(?:\.\d*)|\.\d+)(?:[eE][+-]?\d+)?", line)
#v[0]  0.01
#v[1]  -0.05

I tried something similar in R (but it didn't work) by using the same
regular expression, but got an error:

edm<-grep("[+-]? *(?:\d+(?:\.\d*)|\.\d+)(?:[eE][+-]?\d+)?",txt_line)
#Error: '\d' is an unrecognized escape in character string starting "[+-]?
*(?:\d"

I'm not even sure which function in R most efficiently extracts the values
from "txt_line".  Basically, I want to peel out the values and think I can
use the decimal point to construct the regular expression, but don't know
where to go from here?


--
View this message in context: http://r.789695.n4.nabble.com/extracting-values-from-txt-file-that-follow-user-supplied-quote-tp4632558p4632724.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list