Extract Text Using Regular Expression in R

Posted by Agung Pambudi on
Use the new stringr package which wraps all the existing regular expression operates in a consistent syntax and adds a few that are missing:

Regex. (source: blog.cmstutorials.org)

R> library(stringr)
R> str_locate("aaa12xxx", "[0-9]+")
# start end
# [1,] 4 5
R> str_extract("aaa12xxx", "[0-9]+")
# [1] "12"


R> gsub("[^0-9]", "", "aaa12xxxx")
# [1] "12"

No comments:

Post a Comment