top | item 9752841

(no title)

orangeduck | 10 years ago

Lua has an alternative to Regex which is an extension of the C patterns (used in printf etc).

http://lua-users.org/wiki/PatternsTutorial

At first I found it a bit confusion but they're actually pretty great. For me at least 90% of the tasks I want to do with regex can be done with scanf, and Lua's little extension covers the remaining 10% quite well.

I know lots of people often say that it would be nicer to use functional composition for regex instead of strings because strings are too confusing, but I disagree with this. The confusion of regex to me is not from the string representation, it is that some characters are "special" while others are "normal" (including whitespace). At first it appears that most characters are "normal" and so you can start from some example and generalize the string until it matches all the things you want - but once you start putting parenthesis and such in you start to realize that most of the string wont be matched "normally" and it is better to start thinking like a grammar and write it from scratch. This double thinking is pretty annoying.

For this reason, to me the Lua patterns are really the only alternative I've come across to regex that I've liked. They've got nice compact expressive syntax, can really easily do most of the matching tasks I need due to the scanf base, (almost) all the "special" characters begin with %, and the complex cases can still be matched.

discuss

order

hamstergene|10 years ago

I read the tutorial you linked and I see no difference between Lua patterns and regexes, except that '\' has been replaced with '%', and '*?' with '-'. The only thing in common with scanf is adopting '%' as control character, otherwise that's the same old regular expressions: '%d' matches just one digit, not whole signed integer like in scanf.