Regular expressions, an example

February 3, 2011
By

(This article was first published on The Data Monkey, and kindly contributed to R-bloggers)

Why regular expressions are your friend. This is written in Stata but applies to any language where regular expressions exist.

Original version
if length(“`qx'”)==3 { /*ex: 5q0*/
local a = substr(“`qx'”, 1, 1)
local b = substr(“`qx'”, 3, 1)
}
else if substr(“`qx'”, 2, 1) == “q” & length(“`qx'”)==4 { /*ex: 5q20*/
local a = substr(“`qx'”, 1, 1)
local b = substr(“`qx'”, 3, 2)
}
else if substr(“`qx'”, 3, 1) == “q” & length(“`qx'”)==4 { /*ex: 10q5*/
local a = substr(“`qx'”, 1, 2)
local b = substr(“`qx'”, 4, 1)
}
else if length(“`qx'”)==5 { /*ex: 10q20*/
local a = substr(“`qx'”, 1, 2)
local b = substr(“`qx'”, 4, 2)
}

Regular expression version
foreach q in `qx’ {
local a = regexr(“`q'”,”q.+”,””)
local b = regexr(“`q'”,”.+q”,””)
}

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...