Extract Text Using Regular Expression in R

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"


or


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

SHARE

About Agung Pambudi

I am a Blogger with passion in business.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment