[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  Wildcards in regexp

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?

Wildcards in regexp

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page


Posted by Kora   (1 post)  [Biography] bio
Date Mon 12 Mar 2012 04:55 AM (UTC)  quote  ]

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"?
[Go to top] top

Posted by Nick Gammon   Australia  (18,770 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Tue 13 Mar 2012 04:22 AM (UTC)  quote  ]
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
[Go to top] 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.


892 views.

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]