Jump to content




Lua identifier

lua

  • You cannot reply to this topic
6 replies to this topic

#1 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 11 July 2016 - 04:44 PM

Does this ('^%a[%w_]+') pattern work for every Lua identifier?

#2 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 11 July 2016 - 05:19 PM

If the identifier starts at the first character of the string, then yeah, almost. I'd use
^[%a_][%w_]*
though, since underscore can lead the way too and you don't need more than 0 of the second class.

#3 Sewbacca

  • Members
  • 450 posts
  • LocationStar Wars

Posted 11 July 2016 - 06:10 PM

^[%a_][%w_]*

I think
^[%a_][%w_]-
Is better, because * goes to the last found of [%w_], and - goes to the last found of [%w_] without gaps, <-- ^^
but thanks ^^.

Edited by Sewbacca, 14 July 2016 - 03:43 PM.


#4 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 11 July 2016 - 06:46 PM

Come again? Gape? You mean gap? Nope, that's not how it works. [%w_]* comes after [%a_], so whatever [%w_]* matched must come after whatever [%a_] matched. [%w_]- will match the least possible characters, which since you already have an [%a_] in there will be an empty string.

#5 Exerro

  • Members
  • 801 posts

Posted 11 July 2016 - 07:25 PM

Not quite, it won't match an already matched character, so the '-' section will just match one more character. You'll be limited to just 2 character identifiers, no more or less, if you do it with a '-' instead of '*'. LBPHacker's first suggestion is the way to do it.

#6 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 11 July 2016 - 07:37 PM

Nope, the '-' section won't match anything. As I've stated above, '-' will match the least possible number of characters, which is, in this case, 0. The '-' is used in situations in which it's followed by other sections, such as
local command, arguments = command_line:match("^%s*(.-) (.*)%s*$")
This way only the first chunk of non-whitespace characters is stored in command and the rest in arguments, for '-' matches the least possible number of characters. Yes, it could match an empty string had the leading spaces not been already matched by %s*, thus leaving only non-spaces for (.-) to match.

Edited by LBPHacker, 11 July 2016 - 07:37 PM.


#7 Exerro

  • Members
  • 801 posts

Posted 11 July 2016 - 07:41 PM

Ah, you're right. For some reason I thought the '-' matched at least one character, but it can match none as well. My bad.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users