[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Patterns: Why are anchors not character classes?
- From: "John Hind" <john.hind@...>
- Date: Tue, 14 Jul 2015 17:21:32 +0100
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
If the start and end anchors behaved like character classes we could do
better. For regularity they probably should be %^ and %$ rather than the
existing special characters (and yes, I did have to look those up - as far
as I can see the symbols have absolutely no mnemonic value). Then we could
write:
ps = "1,22,33,4444"
for n in ps:gmatch("(%d+)[,%$]") do
print(ls)
end
The other thing I'd like to see is some "standard" patterns as constants in
the string library for the common functions that a C programmer for example
will miss ("trimright", "trimleft" and "trim", for example).
---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus