Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ Lua ➜ Wildcards in regexp

Wildcards in regexp

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Kora   (1 post)  Bio
Date Mon 12 Mar 2012 04:55 AM (UTC)

Amended on Mon 12 Mar 2012 05:52 AM (UTC) by Kora

Message
I'm writing a script which I would like to respond to 'random *'. I would also like 'rando *' or 'rand *' to fire the script, but not 'randm *'. It's important the alias can take and recognize a parameter, so I need to be able to put a wildcard after whatever I do.

"^rand(om?)?$" gives me what I want, but
"^rand(om?)? (.*)$" gives me the following error:

Quote:
Run-time error
Plugin: Random (called from world: Inferno)
Function/Sub: GenerateNumber called by alias
Reason: processing alias "RandomParameter"
[string "Plugin"]:3: bad argument #2 to 'random' (number expected, got nil)
stack traceback:
[C]: in function 'random'
[string "Plugin"]:3: in function <[string "Plugin"]:1>
Error context in script:
1 : function GenerateNumber(name, line, wildcards)
2 : n = tonumber(wildcards[1])
3*: Note("Random number [0-" .. wildcards[1] .. "]: " .. math.random(0, n))
4 : end -- function

Clearly, it's having trouble picking up the wildcard now. This makes no sense.

To clarify, "^random (.*)$" without the quantification gives me no error. Neither does "^rando?m? (.*)$" but that will match 'randm *'.

Does anyone know what's wrong?

EDIT: Okay, so I thought about it and decided to try picking up wildcards[2] instead and that's fixed my problem. However, I'm still confused as to why. Does it just take anything in parentheses and add it to the wildcards array? And if so, why is wildcards[1] nil and not "om"?
Top

Posted by Nick Gammon   Australia  (23,166 posts)  Bio   Forum Administrator
Date Reply #1 on Tue 13 Mar 2012 04:22 AM (UTC)
Message
Things in paranthese are "captures" and end up in the wildcards array. Unless, that is you turn it off, like this:




^rand(?:om?)? (.*)$


The ?: says "don't capture this group".

What might be better is to name the desired wildcard and then it doesn't matter what number it is, eg.


^rand(om?)? (?P<rand>.*)$


Now your code can say:


n = tonumber(wildcards.rand)


That pulls in the wildcard named "rand".

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


10,907 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.