What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Gettin data from "/" commands

Pages: (2): « First « 1 [ 2 ] Last »
Gettin data from "/" commands
Author: Message:
CookieRevised
Elite Member
*****

Avatar

Posts: 15517
Reputation: 173
– / Male / Flag
Joined: Jul 2003
Status: Away
RE: Gettin data from "/" commands
I know this is an old thread, but here are some important things I never bothered to reply with before because it is rather much. But still, since this topic comes up now and then:

quote:
Originally posted by markee
And finally [\s\S] is simply the equivalent of "."
Nope, it's not though. There is a difference.

"." matches any single character, except "\n".
"[\s\S]" matches any single character, including "\n".

Build-in Messenger Plus! commands do allow for "\n" in their parameters.
So "[\s\S]*" should definitely be used for the parameter string syntax instead of ".*".

eg: /me is<ctrl+enter>testing.

---------

markee, I actually don't agree upon those other comments and examples either though (about defining seperate parameters directly in that first regular expression).
Your third example doesn't even work at all.

Anyways, only the first spacing character is ignored with build-in Plus! commands. Every other character following that first spacing character is actually part of the parameter (and _not_ part of the seperator anymore in case it is another spacing character). So the captured parameter string (RegExp.$2) should contain all the spacing characters, except the first one.

What you later do with that captured parameter string (RegExp.$2) is up to a next procedure. Eg: trimming the spaces, splitting it up into multiple parameters, etc.
This also makes for much better practice and versatile, yet consistant, code imho. Because then you have a clear and seperated way of handling the parameter part, across multiple commands which can have each their own syntaxis. And you will also have an easy way to tell Plus! (and consequently the user) if the command was recognized but if there were some parameters missing or wrong.

Defining those seperated parameters directly in to that first main regular expression makes this harder in many (but granted, not all) cases.









However, you're correct about the possebility of the slash being part of the command as long as it isn't the second character.
But so is a <tab> character! And speaking of special characters, <hard space> (\xA0 = <alt+0160> or <alt+255>) can't be part of it as Plus! does consider this to be a spacing character.

So, the second part, "\S*", isn't entirly correct either, it should actually be "[^ \n\r\v\xA0]*".
notice the lack of \t and \f, and the addition of \xA0.

This has similar implications for the other parts too, see below:

---------

Similar, the second character, the one right after the first slash, isn't actually correctly defined as "[^\s\/]" either.
We know that:
  //test
  /<space>test
     => are NOT recognized as a possible command strings, but as literal texts (special case with //test though)

But in addition:
  /<hard space>test
     => is NOT recognized as a possible command string either as Plus! considers <hard space> to be a spacing character (RegExp rules do not).
  /<tab>test
  /<form feed>test
     => is recognized as a possible command string though, as Plus! does not consider a <tab> or <form feed> to be a spacing character (RegExp rules do).

Thus, the first part, "[^\s\/]", should actually be "[^ \n\r\v\xA0\/]".

---------

Then there is the seperation character between the command and its parameter.
There should actually be only 1 spacing character and that is always ignored, no matter what spacing character it is.
The rest of the spacing characters, if they are present, belong to the parameter string itself.

This single spacing character can be a <space>, but it can also be a <newline> (\n), a <return> (\r), a <vertical tab> (\v), and the <hard space> character (\xA0).
eg: /me<ctrl+enter>is testing.

However, it can not be a <tab> (\t) or a <form feed> (\f) as those are considered non-spacing characters by Plus!.
eg: /me<ctrl+tab>is testing.

So instead of "\s*" for the seperation character, you should use "[ \n\r\v\xA0]?".

But, if you implement the above syntaxis for the command string (first and second part), you could instead use "[\s\xA0]?" for the seperation character because the trailing <tab> and the <form feed> would already be captured as part of the command string itself in case they occur. But be carefull, you can not simply replace the entire command part "\/([^ \n\r\v\xA0\/][^ \n\r\v\xA0]*)" with the literal command string if you only want to check upon one command. For that, see below.








CONCLUSSION

As such: to seperate the command from its parameter string, just like Messenger Plus! does, one should use:
code:
if (/^\/([^ \n\r\v\xA0\/][^ \n\r\v\xA0]*)[ \n\r\v\xA0]?([\s\S]*)/.exec(sMessage)) {
    var command = RegExp.$1.toLowerCase();
    var parameter = RegExp.$2;
or
code:
if (/^\/([^ \n\r\v\xA0\/][^ \n\r\v\xA0]*)[\s\xA0]?([\s\S]*)/.exec(sMessage)) {
    var command = RegExp.$1.toLowerCase();
    var parameter = RegExp.$2;
Of course, most people would use simpler regular expressions or comparissons, but this one is the most correct one I can think of to mimic command handling exactly as it is done in Messenger Plus! internally. Any other syntax does not behave the same (especially not when you use \s and \S).

--

If you have only one command in your script you can not simply replace the first capture syntax with the command string. Instead you should use:
code:
if (/^\/mycommand(?:[ \n\r\v\xA0]([\s\S]*))?$/.exec(sMessage)) {
    var parameter = RegExp.$1;
This to avoid non-spacing characters like the <tab> and <form feed> (in regards to the Plus! command syntax that is) to be handled as spacing-characters, but yet to allow for the other spacing characters and ignoring them in the capture.

Notice that we do use "$" here to indicate the end of the string because we also used "?" to indicate that the match can happen 0 or 1 time.
If we didn't use "$" then the "?" would be interpreted as a non-greedy identifier. Using "{0,1}" instead will not work here either.
Also notice the use of "?:" to ignore the single seperator character between the command and the parameter in the capture.





TO RECAP

For more than one command:
code:
if (/^\/([^ \n\r\v\xA0\/][^ \n\r\v\xA0]*)(?:[ \n\r\v\xA0]([\s\S]*))?$/.exec(sMessage)) {
if (/^\/([^ \n\r\v\xA0\/][^ \n\r\v\xA0]*)[ \n\r\v\xA0]?([\s\S]*)/.exec(sMessage)) {
if (/^\/([^ \n\r\v\xA0\/][^ \n\r\v\xA0]*)[\s\xA0]?([\s\S]*)/.exec(sMessage)) {
    var command = RegExp.$1.toLowerCase();
    var parameter = RegExp.$2;
All will produce the same behaviour and result, where the first and longest form is the most versatile because you can simply replace the command capture with the literal command string as shown below:

For a single command:
code:
if (/^\/mycommand(?:[ \n\r\v\xA0]([\s\S]*))?$/.exec(sMessage)) {
    var parameter = RegExp.$1;
.-= A 'frrrrrrrituurrr' for Wacky =-.
12-07-2009 01:39 AM
Profile PM Find Quote Report
Pages: (2): « First « 1 [ 2 ] Last »
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On