[R] Regular Expressions in grep

Noia Raindrops noia.raindrops at gmail.com
Tue Aug 21 21:38:23 CEST 2012


'grep' does not change strings. Use 'gsub' or 'regmatches':

# gsub
Front <- gsub("^.*?([1-9][0-9]*\\.).*?$", "\\1", a)
End <- gsub("^.*?(\\.[0-9]*[1-9]).*?$", "\\1", a)
# regexpr and regmatches (R >= 2.14.0)
Front <- regmatches(a, regexpr("[1-9][0-9]*\\.", a))
End <- regmatches(a, regexpr("\\.[0-9]*[1-9]", a))

Front
## [1] "1020."
End
## [1] ".9092"

-- 
Noia Raindrops
noia.raindrops at gmail.com




More information about the R-help mailing list