lua-users home
lua-l archive

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


Roberto Ierusalimschy said:


> You can use empty captures with string.match to know where you are.
> string.match(s, "()pat()") is almost equivalent to string.find(s, "pat").
> (Usually you can capture more useful and efficient positions.)
>

Indeed. But you need to know what the pattern is in order to get the
correct result position.

Consider again the lexer example I provided. The lexer library does not
know nor care how many captures are being produced by the pattern (and it
does not require that the pattern be manipulated to insert ()'s either.)
It simply feeds the captures into the associated function; however, it
needs to know where the pattern match ended in order to figure out which
is the longest lex. Admittedly, this wouldn't work correctly with trailing
context patterns, but in practice this has not been much of a limitation.
Making it work with the "new" string.find would be a nuisance.

If you don't want any captures from a pattern, you can simply not put any
parentheses into it. So I don't see any good reason why string.find
shouldn't return any captures it runs into.

R.