lua-users home
lua-l archive

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


> I have been using LPeg happily for quite a while for parsing comma
> separated value (CSV) data and would like to share a bit of experience
> I've gained which resulted in a few small but important tweaks to the
> example grammar given on the LPeg page [1]. [...]

Many thanks for your suggestions. (My implementation follows
RFC 4180, which does not allow tabs as separators or spaces around
quoted fields.)


> In the definition for field that I use every day, where the value for
> field has this,
> 
> lpeg.Cs(((lpeg.P(1) - '"') + lpeg.P'""' / '"')^0)
> 
> I have lpeg.C instead of lpeg.Cs.  I don't remember why I did this but
> it seems to work either way.

The lpeg.Cs substitutes single quotes for double quotes, therefore
unescaping quotes. Try this example:

local ex1 = '"a ""field"" containing quotes",123,3.14,2.717'

-- Roberto