lua-users home
lua-l archive

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


On Thu, Jun 28, 2012 at 6:00 PM, Andrew Starks <andrew.starks@trms.com> wrote:
> On Jun 28, 2012, at 17:35, Daurnimator <quae@daurnimator.com> wrote:
>
>> On 29 June 2012 08:07, Geoff Leyland <geoff_leyland@fastmail.fm> wrote:
>>> Hi,
>>>
>>> When you're parsing csv files, it would be useful to look for the end of a field with the pattern "[,$]" so that you can find the end of the last field as well as the ends of the previous fields.  Trouble is, this doesn't seem to work.  Can anyone tell me what I'm missing?
>>>
>>> (I looked at [1] and as far as I can tell, that code expects the last field to end with a comma)
>>>
>>> Cheers,
>>> Geoff
>>>
>>> [1] http://lua-users.org/wiki/CsvUtils
>>>
>>>
>>
>> Yes using $ or ^ in a class would be useful, but sadly not possible.
>> You can however use the frontier pattern, it'll match the start or end
>> of a string as a null byte.
>> So you can use %f[,%z]
>> See the 5.2 manual for more info.
>>
>
> The frontier pattern doesn't help if you need to escape inner quotes. Like:
>
> " test ""this is cool!"""
>
> Or it does and I'm not smart enough.
>
> I looked at LPEG and even used the provided example, but it was no
> match for Quickbooks exports. I couldn't find one that did work, but
> if you want my CSV iterator, which I'm not claiming is awesome, let me
> know.
>
> Andrew Starks
>

I don't know LPEG at all but I'm sure you could construct something
equivalent to the regex "(,|$)", no?

/s/ Adam