I have a string:
s <- "test.test AS field1, ablh.blah AS field2, faslk.lsdf AS field3"
I want to convert to:
"field1, field2, field3"
I know that the regular expression (\w+)(?:,|$)
will extract the strings I want ('field1,' etc) but I can't figure out how to extract it with gsub
.
## Preparation
s <- "test.test AS field1, ablh.blah AS field2, faslk.lsdf AS field3"
pat <- "(\\w+)(?:,|$)" ## Note the doubly-escaped \\w
## Use the powerful gregexpr/regmatches one-two punch
m <- gregexpr(pat, s)
paste(regmatches(s, m)[[1]], collapse=" ")
# [1] "field1, field2, field3"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With