lua-users home
lua-l archive

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


It was thus said that the Great albertmcchan once stated:
> 
> On Jan 25, 2018, at 8:03 PM, Sean Conner <sean@conman.org> wrote:
> 
> >  If you are looking for a final "and" (which ends the input), then this
> > works:
> > 
> >    last_and = P"and" * P(-1)
> >    char     = R("\0\96","b\255")^1
> >                 + -last_and * P"a"
> >    pat      = C((char)^0) * last_and
> > 
> >    print(pat:match(string.rep("this and that land",400) .. "and"))
> > 
> 
> your example also shows usefulness of undo lpeg.U (if exist):
> 
> pat = C( P(1)^3 * U(3) * #P'and' )

  Even if lpeg.U() existed, I don't think this would do what you expect it
do to.  

	read and understand this

The P(1)^3 will consume past the word "this".  

	read and understand this
	                        ^

Then what?  the U(3) would back up three characters:

	read and understand this
                            ^

And then it would fail because the expression #P'and' fails.

This brings up another point---the Lua pattern

	(.*)and(.*)

applied to "read and understand this" will return

	"read and underst"
	" this"

If this is what you want, then the Lua pattern is probably what you want. 
If this isn't what you want, and what you really wanted was

	"read"
	"understand this"

Then you need to rethink what you are attempting to do (and adjust the
patterns or LPeg code accordingly).

  -spc