[R] Minimal match to regexp?

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


On 25/01/2023 7:19 p.m., Duncan Murdoch wrote:
> 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 .*?

Sorry, that was a dumb question.  Of course grep returned the whole 
thing.  I should be using regexpr() or some related function to extract 
the match.

Duncan Murdoch



More information about the R-help mailing list