@Greg Snow was kind enough to introduce me to pattern matching using regular expressions. I used his advice to do the following:
sql <- "SELECT a, b, (q + r) AS c, (s + t) AS d FROM tbl WHERE x=y" sql <- gsub("^.*SELECT *(.*?) +FROM.*$", "\\1", sql) "a, b, (q + r) AS c, (s + t) AS d"
I was curious and tried to expand this logic to replace "anything after the decimal point with and including" AS ":
sql<- gsub(" \\(.*AS", "\\1", sql) "a, b, d"
I wanted him to return "a, b, c, d". However, I see what happens - it matches my pattern across the line, starting with a comma after "b" and ending with its second AS, not the first.
My question is, how can I match a pattern multiple times on the same line? I know that I am doing something wrong with the syntax.
source share