lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> I'm trying to make a regex that splits a string of elements delimited by
> some character.  The following doesn't work.  I can think of other ways but
> I'd like to know why this doesn't work, and also why strfind is returning an
> apparently illegal result (start greater than end).
>
>     > print(strfind("hello:there", "(.-)%:?"))
>     1       0

This seemed like a nice brainteaser with which to start the day, so I had a
play. My conclusion is that this is a perfectly reasonable result:

The minimum way in which to match (.-) is by matching the empty string at
the start of "hello:there"; then, an optional : also matches the empty string.

The return value "1,0" indicates that the string found starts at position
one, and is of length zero (a return of 1,1 would indicate that "h" had been
matched).

Not what you want, therefore, but perfectly legal. How about

> print(strfind("hello:there" .. ":", "(.-)%:"))
1       6      hello

i.e. tack an extra delimiter on to the end to make sure that you do get one,
and make the delimiter mandatory. This negates the effect of the -
"overriding" the ?.

Still, this suggests that - isn't really minimal matching. Does this count
as a bug?

-- 
http://sc3d.org/rrt/ | free, a.  already paid for (Peyton Jones)