mIRC Homepage
Posted By: Anonymous regular expressions - regsub small problem - 23/10/05 03:55 PM
hi. i'm trying to replace all the smile) and ;D , :P etc with colored versions of them with the help of regular expressions. do you have any suggestions why the code below doesn't work?
it sais Invalid parameters: $regsub (line xxx, script.mrc)
i logically think it should work.
other then that, i have another question: what's the /( at the beggining for and the )/g at the end? regular expressions don't have such things. is this a mirc specific? also, the parameter \1.... i haven't seen it before, usualy it's $1- right? is it an alias?
anyway, here's the code below (i don't have ay clue why it doesn't work). i need the smilies to be colored red with a yellow background.
Code:
if ($regsub(testname2,$1-,/((:|;)((\)+)|(D+)|(P+)|(>+)|(\(+)|(<+))))/g,4,8\1,%bbb) == 0) %bbb = $1-
say %bbb
haltdef etc... (the regsub is the problem.. not this code :) )


thanks everybody.
Posted By: Sigh Re: regular expressions - regsub small problem - 23/10/05 04:41 PM
Try the following:

Code:
  var %a = 4,8\1
  say $(,,$regsub($1-,/([:;][()<>DP])/g,%a,%a)) %a
  halt


You had a comma in the replacement parameter that was being evaluated by mirc as a parameter separator

The / and / enclose the expression, g is a modifier that tells it to keep matching past the first successful match (replace all smileys) and the () captures the smileys so you can use \1 to reference what was matched in the replacement part
Posted By: Anonymous Re: regular expressions - regsub small problem - 23/10/05 05:09 PM
your version works fine.
thanks for the explanation and the shorter code smile
I think there's a problem with the ) in your original regex too. mIRC parses the command first, and if it finds the matching ) for the $regex( then all behind it is ignored for the regex. mIRC doesn't care about the backslash in \) so if you don't have a matching \( before in your regex, mIRC will most likely fail.

Here's a small example, you'll figure it out why it does what it does smile
Code:
//Echo -s $regex(abcd,/[^p]/g)
//Echo -s $regex(abcd,/[^)]/g)
//Echo -s $regex(abcd,/[^) ]/g)


solutions: use \x28 or \051 or \51 for \( and \x29 or \052 or \52 for \) , "\ $+ $chr(40) $+ " works too but it's clumsier imo.

Mind this only works in the regex part, not in the replacement text part in a $regsub frown
Posted By: Anonymous Re: regular expressions - regsub small problem - 24/10/05 03:26 PM
this just shows that mirc's regular expressions need a lot of work frown
no wonder my original solution wasn't good.
This has nothing to do with the implementation of regular expressions (which is a separate library, PCRE, anyway). It has to do with how mirc parses identifiers in general. \) is a regex escape sequence (which indeed works with mirc's regex too) but it doesn't escape the special meaning of ")" in mirc: just as in

//echo -a $str(\),3)

"\)" doesn't prevent mirc from returning an error. Can you see why this example errors?

To pass ")" characters (or "\)" sequences for that matter) to an identifier, either escape them the mirc way (use ... $+ $chr(41) $+ ...) or put them in a variable and use that variable as an identifier parameter. For example:

//var %a = \) | echo -a $str(%a,3)

or

//echo -a $str(\ $+ $chr(41),3)
© mIRC Discussion Forums