This should get what you want. The only assumption is that all the lines you want to use for single quotes contain a colon (otherwise, how should we distinguish between '01: ANTIG_CLIENTE <= 4' from ' when ANTIG_CLIENTE <= 8 then ' , both of which are enclosed in single quotes?):
> regmatches(la,gregexpr("'[^']*:[^']*'",la)) [[1]] [1] "'01: ANTIG_CLIENTE <= 4'" "'02: ANTIG_CLIENTE <= 8'" "'99: Error'"
Basically, we are trying to return all expressions (hence gregexpr instead of regexpr ) of the form of a single quote, something other than a single quote, a colon, something other than a single quote, a single quote.
If you want to exclude single quotes in what is returned, you will need look-ahead and look-behind, which requires R to interpret your regular expression as perl:
> regmatches(la,gregexpr("(?<=')[^']*:[^']*(?=')",la,perl=T)) [[1]] [1] "01: ANTIG_CLIENTE <= 4" "02: ANTIG_CLIENTE <= 8" "99: Error"
source share