Are there regular expression libraries?

Naturally! But see if you really cannot do the task with Lua's built-in string patterns. Although simpler than traditional regular expressions, they are powerful enough for most tasks. In most cases, you can understand a Lua string pattern by mentally replacing '%' with '\'; the advantage of % of course is that it does not itself need escaping in C-style strings such as Lua uses.

For example, extracting two space-separated integers from a string:

local n,m = s:match('(%d+)%s+(%d+)')
n = tonumber(n)
m = tonumber(m)

There is a string recipes page on the Wiki.

If you need more conventional regular expressions such as PCRE, see lrexlib.

Another library to consider instead of PCRE is Lpeg.

Here is a comparison of PCRE and Lpeg for a particular task:



Back