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 David Heiko Kolf once stated:
> Gavin Wraith wrote:
> 
> If you want it to return nil, make sure that your pattern does not match
> in this case.  You would simply have to change your definition of WORDS
> to require at least one captured word:
> 
>   local WORDS = BLANK*(C(WORD)*BLANK)^1*END
> 
> This should then work as expected.

  You can also have LPeg return the constructed table for you.  Using your
original definition of WORDS:

	local WORDS = BLANK*(C(WORD)*BLANK)^0*END

  Try the following:

	local Ct = lpeg.Ct
	local TWORD = Ct(WORDS)

	x = TWORD:match" one two three "
	y = TWORD:match"  "
	z = TWORD:match""

Ct() will collect the captures of the given pattern into a table, which it
returns.  Since BLANK does no capture, its patterns aren't stored.  

  -spc