Skip to content
Prev 393733 / 398503 Next

Minimal match to regexp?

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