RegEx in R to extract values between square brackets

Except for RegEx always being a pain, one would think that parsing a string in R is a piece of cake. Well, it is – sort of…

So here goes:

install.packages("stringr")
library(stringr)

formulas <- c(
    "[000275]-[007825]-[007826]-[007827]",
    "[bru]-[007303]-[007301]-[007305]-[007307]"
    )

variables <- regmatches(formulas, gregexpr("\\[.+?\\]", formulas))

Advertisements
Posted in R

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s