Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 443
Badesign Avatar asked Jan 10 '23 06:01

Badesign


2 Answers

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

like image 188
zx81 Avatar answered Jan 19 '23 12:01

zx81


Actually it will match nothing. Because there is an error: w- this is a start of a text range and it is out of order. So it should be %w\- instead.

^[%w\-.]+$

Means:

  • ^ assert position at start of the string
  • [%w\-.]+ match a single character present in the list below
    1. + Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
    2. %w a single character in the list %w literally (case sensitive)
    3. \- matches the character - literally
    4. . the literal character .
  • $ assert position at end of the string

Edit

As the OP changed the question and the tags this answer no longer fits as a proper answer. It is POSIX based answer.

As @zx81 comment:

  • %w is \w in Lua which means any alphanumeric characters plus "_"
like image 41
Jorge Campos Avatar answered Jan 19 '23 12:01

Jorge Campos