[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: The shortest possible sequence match with Lua regular expressions
- From: Patrick Donnelly <batrick@...>
- Date: Fri, 30 Oct 2009 16:53:01 -0400
On Fri, Oct 30, 2009 at 4:43 PM, Joshua Jensen
<jjensen@workspacewhiz.com> wrote:
> I ran into this today, and while I tried to explain it away, I have to admit
> I am mildly confused.
>
> Why does the following match the entire string "/dev/myprog/image/xyz/"?
>
> =string.match('/dev/myprog/image/xyz/', '(/.-/)$)
>
> The documentation states that a - (minus) matches the shortest possible
> sequence.
>
> I hadn't given this any thought until today, as I normally would have
> written '.+(/.-/)$'. That would have yielded the correct result of '/xyz/'.
> When discussing it with a coworker, though, he raised the point that with
> the final / (slash) anchored at the end of the string, the shortest possible
> matching sequence should be '/xyz/'.
>
> I can buy into that argument. What are we missing?
.- will only backtrack if the portion of the pattern following it (/$)
all fail for all characters after the current position. The shortest
sequence simply means that it will attempt to match using the least
amount of characters first. This is naturally different from a greedy
repetition which will try to match using the most amount of characters
first.
--
-Patrick Donnelly
"Let all men know thee, but no man know thee thoroughly: Men freely
ford that see the shallows."
- Benjamin Franklin