[R] Minimal match to regexp?

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Thu Jan 26 01:42:51 CET 2023


Perhaps

sub( "^.*(a.*?a).*$", "\\1", x )


On January 25, 2023 4:19:01 PM PST, Duncan Murdoch <murdoch.duncan using gmail.com> 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 .*?
>
>Duncan Murdoch
>
>______________________________________________
>R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-help mailing list