mIRC Home    About    Download    Register    News    Help

Print Thread
regular expressions - regsub small problem #133675 23/10/05 03:55 PM
A
Anonymous
Unregistered
Anonymous
Unregistered
A
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.

Re: regular expressions - regsub small problem #133676 23/10/05 04:41 PM
Joined: Aug 2003
Posts: 314
S
Sigh Offline
Fjord artisan
Offline
Fjord artisan
S
Joined: Aug 2003
Posts: 314
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

Re: regular expressions - regsub small problem #133677 23/10/05 05:09 PM
A
Anonymous
Unregistered
Anonymous
Unregistered
A
your version works fine.
thanks for the explanation and the shorter code smile

Re: regular expressions - regsub small problem #133678 24/10/05 12:22 PM
Joined: Apr 2003
Posts: 701
K
Kelder Offline
Hoopy frood
Offline
Hoopy frood
K
Joined: Apr 2003
Posts: 701
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

Re: regular expressions - regsub small problem #133679 24/10/05 03:26 PM
A
Anonymous
Unregistered
Anonymous
Unregistered
A
this just shows that mirc's regular expressions need a lot of work frown
no wonder my original solution wasn't good.

Re: regular expressions - regsub small problem #133680 24/10/05 04:15 PM
Joined: Jan 2003
Posts: 2,523
Q
qwerty Offline
Hoopy frood
Offline
Hoopy frood
Q
Joined: Jan 2003
Posts: 2,523
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)


/.timerQ 1 0 echo /.timerQ 1 0 $timer(Q).com