Search code examples
lualua-patterns

Repetitive regular expression in Lua


I need to find a pattern of 6 pairs of hexadecimal numbers (without 0x), eg. "00 5a 4f 23 aa 89"

This pattern works for me, but the question is if there any way to simplify it?

[%da-f][%da-f]%s[%da-f][%da-f]%s[%da-f][%da-f]%s[%da-f][%da-f]%s[%da-f][%da-f]%s[%da-f][%da-f]


Solution

  • Lua patterns do not support limiting quantifiers and many more features that regular expressions support (hence, Lua patterns are not even regular expressions).

    You can build the pattern dynamically since you know how many times you need to repeat a part of a pattern:

    local text = '00 5a 4f 23 aa 89'
    local answer = text:match('[%da-f][%da-f]'..('%s[%da-f][%da-f]'):rep(5) )
    print (answer)
    -- => 00 5a 4f 23 aa 89
    

    See the Lua demo.

    The '[%da-f][%da-f]'..('%s[%da-f][%da-f]'):rep(5) can be further shortened with %x hex char shorthand:

    '%x%x'..('%s%x%x'):rep(5)