lua-users home
lua-l archive

Re: Patterns: Why are anchors not character classes?

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


On Tue, Jul 14, 2015 at 9:21 AM, John Hind <john.hind@zen.co.uk> wrote:
> I've just had another bout of wrestling with patterns, probably the most
> obscurantist part of Lua and the only bit I've never been able to learn and
> always have to get "Programming in Lua" off the shelf. Take the simple case
> of parsing a comma-separated list, the kludge I keep coming back to is
> concatenating a terminating comma onto the source string before applying the
> pattern:
>
> ps = "1,22,33,4444"
> ps = ps .. ","
> for n in ps:gmatch("(%d+),") do
> print(ls)
> end
Why not:
for s in ('1,22,33,4444') : gmatch '%d+' do print ( s ) end
or
for s, c in ('1,22,33,4444') : gmatch '(%d+)(,?)' do print ( s, c ) end
If you need more than either of the above, have you tried LPeg?
http://www.inf.puc-rio.br/~roberto/lpeg/lpeg.html
-Parke

AltStyle によって変換されたページ (->オリジナル) /