lua-users home
lua-l archive

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

On Tue, Jul 14, 2015 at 1:42 PM, Paul K <> wrote:
> Indeed ;). I was thinking about a more complex pattern for extracting
> numbers, which in this case wasn't needed:
> ps = "1,22,33,4444,abc123def"
> for n in ps:gmatch("%f[%w](%d+)%f[%W]") do
>   print(n)
> end
> This would only extract "standalone" numbers (and not those that are
> part of words).

Conversely, if you just simply want everything separated by commas no
matter what, and there's no chance of an escaped comma that is
actually part of what you want to capture, then this simple pattern
will do the trick:

ps = "1,22,33,4444,abc123def"
for item in ps:gmatch("[^,]+") do


This essentially just captures each consecutive run of stuff other than commas.

I would argue that the string library ought to have a proper split
function, but it's not too difficult to write one in Lua. I'll leave
it as an exercise for the reader, and I think there's a whole page on
the wiki about it also.