mIRC Homepage
Posted By: Kaepez Wildcards and $chr(42) - 01/05/06 03:59 AM
I'm trying to get something working that will echo a message whenever the user types a message beginning with an asterisk. I tried using $chr(42) with an "on INPUT" and an if conditional, but unless something else was messed up, mirc treated "$chr(42)*" as a double wildcard, which was definitely not the intended effect.

Can I get a little help? Thanks.
Posted By: SladeKraven Re: Wildcards and $chr(42) - 01/05/06 04:12 AM
Code:
  if ($str($chr(42),2) iswm $1) echo -a $v2
  if ($left($1-,1) == *) echo -a $1-


-Andy
Posted By: MikeChat Re: Wildcards and $chr(42) - 01/05/06 04:14 AM
try this
Code:
on *:input:*:{
  if ($mid($$1,1,1) == $chr(42)) {
    echo -a You typed a $chr(42)
  }
}
Posted By: Mpdreamz Re: Wildcards and $chr(42) - 02/05/06 06:44 PM
on *:input:*: if ($regex($1,/^\*/)) { echo -a You typed a * }
Posted By: MikeChat Re: Wildcards and $chr(42) - 02/05/06 09:43 PM
You know what i think would be great
If when those of you who use and understand regex (or even other advanced coding) include a detailed explanation (for those of us who got headaches after trying to read the help online for regex) so others might benefit from the example you give with info on what it all means.
Posted By: Mpdreamz Re: Wildcards and $chr(42) - 02/05/06 10:35 PM
Your absolutely right my post had little to contribute other then a few bytes saved.

on *:input:*: if ($regex($1,/^\*/)) { echo -a You typed a * }

$regex returns the total matches so the if statement will be false if it doesn't find any since 0 is false.

regular expressions are enclosed in a pair of / so the actual expression is ^\*
^
means the start of the string
\*
since * is a special character to quantify a character zero or more times it needs to be escaped so it loses it special meaning and becomes the litural *
so ^\* means if the string ($1- in this case) starts with a * then return true.

Quantifiers:
*: $regex(a,ab*) returns true because a is followed by zero or more b characters.
The other quantifier is +: which means 1 or more
so
$regex(a,ab+) is false because a isn't followed by one or more b's
Posted By: MikeChat Re: Wildcards and $chr(42) - 03/05/06 12:51 AM
Code:
[color:blue] starting delimiter / [/color][color:red] Start of expression (string) ^ [/color][color:green]Escape Character (required due to special regex meaning for * in this case) \ [/color] the character to test for * [/color][color:blue] End delimiter /[/color]

though I think the OP wanted to check the first character of the first word only (or did I miss something) but the regex would match 1 or more * in the string (did i read that right?)
Posted By: SladeKraven Re: Wildcards and $chr(42) - 03/05/06 12:59 AM
I tested the code and it returns.

0 for test
1 for *test
1 for *test*

So I guess it does match the first caharacter of the first word. smile

-Andy
Posted By: Mpdreamz Re: Wildcards and $chr(42) - 03/05/06 07:24 AM
since we escaped * it has lost its special meaning and is just a litteral * so the regex says:

start of the string followed by an *.
"*hello you!" is true
"hello *you!" is false because the first character in the string is an "h".
Posted By: MikeChat Re: Wildcards and $chr(42) - 03/05/06 09:42 PM
ok so what threw me was
Quote:

on *:input:*: if ($regex($1,/^\*/)) { echo -a You typed a * }

$regex returns the total matches so the if statement will be false if it doesn't find any since 0 is false.


in this case the coding of the regex didnt specify any/all matches, so it checked only the first character in the string (which in this case because only $1 too, not $1-), while $regex() would return any/all matches if it was coded a bit different.... right?
Posted By: FiberOPtics Re: Wildcards and $chr(42) - 03/05/06 11:18 PM
$regex will only return all matches if the /g modifier is specified. In absence of it, it immediately returns on matching (or no matching), meaning it will either evaluate to 0 or 1.

Heh, once I had plans to write the "ultimate regex tutorial" named the "Regex Bible", but then I got a fulltime job and everything changed. mIRC is just a memory now :tongue:

Oh well, happy scripting to all smile
Posted By: Mpdreamz Re: Wildcards and $chr(42) - 04/05/06 07:11 AM
true but even if the g modifier was specified theres always only one start of the string :P

And about your bible i still think you should do a tips & tricks tutorial frown Your posts are missed FO frown
© mIRC Discussion Forums