Skip to content
Prev 303859 / 398502 Next

Regular Expressions in grep

'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"