[R] Minimal match to regexp?

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Thu Jan 26 01:19:01 CET 2023


The docs for ?regexp say this:  "By default repetition is greedy, so the 
maximal possible number of repeats is used. This can be changed to 
‘minimal’ by appending ? to the quantifier. (There are further 
quantifiers that allow approximate matching: see the TRE documentation.)"

I want the minimal match, but I don't seem to be getting it.  For example,

x <- "abaca"
grep("a.*?a", x, value = TRUE)
#> [1] "abaca"

Shouldn't I have gotten "aba", which is the first match to "a.*a"?  If 
not, what would be the regexp that would give me the first match to 
"a.*a", without greedy expansion of the .*?

Duncan Murdoch



More information about the R-help mailing list