Reg-ex problem

Place to get help with not working mods / modding interface.
Post Reply
Pi-C
Smart Inserter
Smart Inserter
Posts: 1651
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Reg-ex problem

Post 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?
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Amarula
Filter Inserter
Filter Inserter
Posts: 511
Joined: Fri Apr 27, 2018 1:29 pm
Contact:

Re: Reg-ex problem

Post 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?
My own personal Factorio super-power - running out of power.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1651
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Reg-ex problem

Post 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
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Amarula
Filter Inserter
Filter Inserter
Posts: 511
Joined: Fri Apr 27, 2018 1:29 pm
Contact:

Re: Reg-ex problem

Post by Amarula »

Another rubber duck question: is there any difference between a local named "name" and a local named "owner" ?
My own personal Factorio super-power - running out of power.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1651
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Reg-ex problem

Post 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.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Bilka
Factorio Staff
Factorio Staff
Posts: 3132
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Reg-ex problem

Post by Bilka »

The "and" is the issue:
Screenshot_20221006_162306.png
Screenshot_20221006_162306.png (68.13 KiB) Viewed 807 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.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

Pi-C
Smart Inserter
Smart Inserter
Posts: 1651
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Reg-ex problem

Post 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
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Post Reply

Return to “Modding help”