[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Newbie question list: Was The perverse vibe of lua-l
- From: Tim Mensch <tim-lua-l@...>
- Date: Tue, 25 Jan 2011 14:35:24 -0700
On 1/25/2011 10:54 AM, Steve Litt wrote:
> By the way, I'm gonna meet the same kind of "screwed if I do, screwed if I
> don't" when I talk about "regex" in Lua. I've seen plenty of documentation
> saying unaided Lua doesn't have Regex, and making a big deal out of the fact
> that Lua has a much smaller footprint because it has no Regex. And yet the
> fact is I can do anything with string.gsub() and string.match() than I can
> with Perl Regex, and Perl Regex is considered the Cadillac of the industry.
Sorry, but Lua simply cannot come close to doing what Perl Regex can,
even ignoring the more obscure features of Perl (of which there are many).
IMO, Lua has pattern matching that is good enough for 98% of situations,
and I will defend the choice to define patterns the way they're defined
to keep Lua small, but honestly there's only a superficial resemblance
between Lua patterns and an actual Regex.
> s = 'barfoofoo'
In Perl, the above expression matches "rfoofoo", and so if Lua supported
Regex it would have matched and printed "baxxx". In Lua it's a nonsense
expression, since "+" doesn't repeat the previous EXPRESSION (ahem,
regular EXPRESSIONs) but the previous CHARACTER or CLASS. That makes it
a different beast.
Even the repeated expression can itself be a complex expression in Perl
(or in Unix grep, or awk, or sed...). For the string
"foofoobarfoofoobar", the expression "^((foo)+bar)*$" matches it using
Perl or Unix extended regular expression matching. Note the ^ and $
characters, meaning that it has to match the entire string.
While IMO it's better (i.e., easier for humans to read and write) to use
something like LPEG for more complex matching, it is unarguably true
that Lua's patterns are not "regular expressions". And I'm not talking a
minor technicality here, but the fundamental fact that regular
expressions involve recursively defining expressions; there's none of
that in Lua pattern matching.
Which is fine for Lua, but please don't call it something it isn't, or
you'll confuse the people who do know the difference.
Though technically you said that you "can do anything with string.gsub()
and string.match() than I can with Perl Regex...", which granted may be
true in your case, but for those of us who do know how to use regular
expressions, it's certainly not. ;)