This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Sewbacca's profile picture

Lua identifier

Started by Sewbacca, 11 July 2016 - 03:44 PM
Sewbacca #1
Posted 11 July 2016 - 05:44 PM
Does this ('^%a[%w_]+') pattern work for every Lua identifier?
LBPHacker #2
Posted 11 July 2016 - 06: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.
Sewbacca #3
Posted 11 July 2016 - 07: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 on 14 July 2016 - 02:43 PM
LBPHacker #4
Posted 11 July 2016 - 07: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.
Exerro #5
Posted 11 July 2016 - 08: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.
LBPHacker #6
Posted 11 July 2016 - 08: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 on 11 July 2016 - 06:37 PM
Exerro #7
Posted 11 July 2016 - 08: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.