Page 1 of 1

Reg-ex problem

Posted: Thu Oct 06, 2022 12:28 pm
by Pi-C
I've run this in a local Lua session:

Code: Select all

Lua 5.2.4  Copyright (C) 1994-2015 Lua.org, PUC-Rio
> x = " 72  asdf"
> id, name = x:match("^%s*(%d+)%s*(.-)%s*$")
> print(id, "|"..tostring(name).."|")
72	|asdf|
I use the same reg-ex to parse the arguments to a command:

Code: Select all

debugging_commands["set-GCKI-owner"] = function(command)
    debugging.entered_command(command)

    local id, owner = command.parameter and
                      command.parameter:match("^%s*(%d+)%s*(.-)%s*$")

AD.show("id", id)
AD.show("owner", owner)
end
Calling that function in the game as

Code: Select all

/set-GCKI-owner  72,  asdf
will get the correct id, but "owner" will always be nil:

Code: Select all

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Entered function for command AD-set-GCKI-owner(tick = 65147, player_index = 1, parameter = " 72,  asdf")
(@__autodrive__/libs/debugging.lua: 754)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

10289.915 Script @__autodrive__/libs/debugging.lua:173: id: 72
10289.915 Script @__autodrive__/libs/debugging.lua:173: owner: nil
Any idea why this works in a regular Lua session but not in the game?

Re: Reg-ex problem

Posted: Thu Oct 06, 2022 12:50 pm
by Amarula
It's been decades since I used regex, but I did notice that the test string "72 asdf" is different from the arguments string "72, asdf". Does regex treat the comma as more whitespace?

Re: Reg-ex problem

Posted: Thu Oct 06, 2022 1:21 pm
by Pi-C
Amarula wrote:
Thu Oct 06, 2022 12:50 pm
It's been decades since I used regex, but I did notice that the test string "72 asdf" is different from the arguments string "72, asdf". Does regex treat the comma as more whitespace?
Sorry, mistake while posting! I originally used "[,%s]*" but dumbed that down to "%s*" to make sure it wasn't that part of the regex which failed. But calling the function without the comma yields the same result:

Code: Select all

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Entered function for command AD-set-GCKI-owner(tick = 47543, player_index = 1, parameter = " 72  asdf")
(@__autodrive__/libs/debugging.lua: 754)
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

13483.931 Script @__autodrive__/libs/debugging.lua:173: id: 72
13483.931 Script @__autodrive__/libs/debugging.lua:173: owner: nil

Re: Reg-ex problem

Posted: Thu Oct 06, 2022 1:39 pm
by Amarula
Another rubber duck question: is there any difference between a local named "name" and a local named "owner" ?

Re: Reg-ex problem

Posted: Thu Oct 06, 2022 1:51 pm
by Pi-C
No, not really. I used "owner" in the mod because it's a more telling variable name than "name". For quick testing in the Lua session, I usually use more generic names. But it doesn't matter, both "id, name" and "id, owner" are just for storing the matches, and so far, the mod function only prints the values of these variables without changing them.

Re: Reg-ex problem

Posted: Thu Oct 06, 2022 2:27 pm
by Bilka
The "and" is the issue:
Screenshot_20221006_162306.png
Screenshot_20221006_162306.png (68.13 KiB) Viewed 932 times
I can't find a good page in the Lua manual to link for this behaviour, but https://www.lua.org/pil/5.1.html and https://www.lua.org/manual/5.2/manual.html#3.4 somewhat describe it with the rules for when the extra return values are discarded.

Re: Reg-ex problem

Posted: Thu Oct 06, 2022 5:20 pm
by Pi-C
Bilka wrote:
Thu Oct 06, 2022 2:27 pm
The "and" is the issue:
Thanks, that was really helpful! I've wrapped the string matching in an if-then statement and it's working as expected now:

Code: Select all

    local id, owner
    if command.parameter then
        id, owner = command.parameter:match("^%s*(%d+)%s*(.-)%s*$")
    end