[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Patterns: Why are anchors not character classes?
- From: Jonathan Goble <jcgoble3@...>
- Date: Tue, 14 Jul 2015 14:07:12 -0400
On Tue, Jul 14, 2015 at 1:42 PM, Paul K <paul@zerobrane.com> 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
print(item)
end
Output:
1
22
33
4444
abc123def
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.