Search code examples
lualua-patterns

What does this pattern ^[%w-.]+$ mean in Lua?


Just came across this pattern, which I really don't understand:

^[%w-.]+$

And could you give me some examples to match this expression?


Solution

  • Valid in Lua, where %w is (almost) the equivalent of \w in other languages

    ^[%w-.]+$ means match a string that is entirely composed of alphanumeric characters (letters and digits), dashes or dots.

    Explanation

    • The ^ anchor asserts that we are at the beginning of the string
    • The character class [%w-.] matches one character that is a letter or digit (the meaning of %w), or a dash, or a period. This would be the equivalent of [\w-.] in JavaScript
    • The + quantifier matches such a character one or more times
    • The $ anchor asserts that we are at the end of the string

    Reference

    Lua Patterns