3

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

^[%w-.]+$

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

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Badesign
  • 37
  • 1
  • 4
  • 2
    That is pretty trivial, I suggest you read up on regex a bit. – leppie Jul 10 '14 at 04:59
  • 1
    [It throws an error.](http://regex101.com/r/zQ9qV5/1) – Avinash Raj Jul 10 '14 at 05:00
  • Actually, this comes from a lua script: local return_domain_name = urlReq:match("^[%w-.]+$") – Badesign Jul 10 '14 at 05:17
  • 1
    That's why the [regex tag info](http://stackoverflow.com/questions/tagged/regex) states that you should always include the regex flavor in the tags. – Tim Pietzcker Jul 10 '14 at 05:49
  • I removed the **regex** tag because Lua pattern is not regular expression, see [its tag infor](http://stackoverflow.com/tags/lua-patterns/info) for more information. – Yu Hao Jul 10 '14 at 05:55
  • @YuHao Thanks for explaining. Great job editing the question and title also. :) – zx81 Jul 10 '14 at 05:57

2 Answers2

6

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

zx81
  • 41,100
  • 9
  • 89
  • 105
3

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 "_"
Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
  • 2
    FYI your answer no longer works. It turns out that `%w` is `\w` in Lua. :) – zx81 Jul 10 '14 at 05:47
  • Hi @zx81 I will edit it to put the information that it is POSIX based answer and it no longer fits as the OP changed the question. Thanks. – Jorge Campos Jul 10 '14 at 11:26