lua-users home
lua-l archive

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

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.

Simple example:

> s = 'barfoofoo'
> print(s:gsub('[^o](foo)+$','xxx'))
barfoofoo       0

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. ;)