lua-users home
lua-l archive

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


On Tue, Mar 4, 2014 at 6:33 PM, Francisco Olarte <folarte@peoplecall.com> wrote:
> Aside from that, what really has left me shocked is the fact that
> s:find(p,5) gives 5,7.

It is useful to parse a string piecewise. You can do it with an
unanchored pattern, by checking that the start of the match is equal
to the index passed to `.find`. It is wasteful, though, because `find`
may walk the whole subject if it doesn't match. It is even more
wasteful if you want to use `match`, since you must first use `find`
to be sure that the match occured at the desired place.

Likewise, LPeg patterns are anchored to the index passed to `lpeg.match(...)`.

PCRE has a flag (PCRE_ANCHORED) to trigger that behavior in patterns
without the caret.

Lua patterns are not regular expressions. They are close, in some
respects, but you shouldn't expect them to work likewise.

That being said, I agree that the documentation could be improved.

You may be interested in these features that AFAIK don't have an
equivalent in regexen.

- %bXY, the balanced pair pattern: "%b()" will match "( (a, b) ( (oo)() ) )"
- %f[charset], the undocumented frontier pattern, which is a bit too
long to describe here. More here:
http://lua-users.org/wiki/FrontierPattern

—Pierre-Yves