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

Just stumbled upon this pattern, which I really don't understand:

^[%w-.]+$

And could you give some examples to match this expression?

+4
source share
2 answers

Valid in Lua, where the %w(almost) equivalent \win other languages

^[%w-.]+$ means matching a string that consists of alphanumeric characters (letters and numbers), dashes, or periods.

Explanation

  • The core ^claims that we are at the beginning of the line
  • A character class [%w-.]corresponds to one character, which is a letter or number (value %w), dash, or period. That would be equivalent [\w-.]in JavaScript
  • +
  • $ ,

Lua

+5

. : w- , . %w\-.

^ [% \-.] + $

:

  • ^
  • [%w\-.]+ ,
    • + : , , []
    • %w % w ( )
    • \- -
    • . .
  • $

Edit

, . POSIX.

@zx81:

  • %w \w Lua, - "_"
+4

Source: https://habr.com/ru/post/1547857/


All Articles