mIRC Home    About    Download    Register    News    Help

Topic Options
#137309 - 11/12/05 06:32 AM Change this to regex?
schaefer31 Offline
Fjord artisan

Registered: 27/08/05
Posts: 525
I have the following code:

Code:
on *:text:*:#Channel:{
  var %c = $strip($1-), %i = 1
  while (%i <= $numtok(%c,32)) {
    if ($len($gettok(%c,%i,32)) == 3) {
      if ($read(smileys.txt, w, $mid($gettok(%c,%i,32),1,1)) && $read(separators.txt, w, $mid($gettok(%c,%i,32),2,1)) && $read(smileys.txt, w, $mid($gettok(%c,%i,32),3,1))) {
        msg # $mid($gettok(%c,%i,32),3,1) $+ $mid($gettok(%c,%i,32),2,1) $+ $mid($gettok(%c,%i,32),1,1)
      }
    }
    inc %i
  }
}


This works fine.

Basically it looks for a smiley within a string of words and if found, it msg's the channel with the same smiley but backwards. (e.g @_x turns to [email]x_@)[/email]


I have a file smilies.txt which contains these characters (on separate lines):
0Oox^-*@u'><
Any of these can be the first and last character.



And also separators.txt which contains these characters (on separate lines):
_ .
One of those has to be the middle character


How can I change the code to use regex instead? Or should I even bother? My brain can't begin to comprehend the use of it, but I would like to learn it eventually. Perhaps this would give me a jumpstart.

Any help appreciated.

Top
#137310 - 11/12/05 07:25 AM Re: Change this to regex?
FiberOPtics Offline
Hoopy frood

Registered: 05/02/04
Posts: 2019
Loc: Leuven, Belgium
/*

Usage:

$smiley(<string>)[.prop]
/smiley <string>

Examples:

//echo -a $smiley(o.x and @_u will be changed)
--> x.o and u_@ will be changed

//echo -a $smiley(o.x and @_u will be changed).del
--> x.o u_@

/smiley .
--> .

*/

Code:
alias sm.set return [-0Oox^*@u'&gt;&lt;]
alias sm.sep return [_.]

alias smiley {
  var %a, %b = (?&lt;=\s|^)( $sm.set )( $sm.sep )((?1))(?=\s|$)
  %b = $regsub(sm,$1-,/ $iif($prop,.*? %b |.*,%b) /xg,\3\2\1 $chr(32),%a)
  $iif($isid,return,echo -a) %a
}


Be careful when adding more characters to the sm.set/sep identifiers, as some have a meaning in a character class. If you're unsure, just ask.
_________________________
Gone.

Top
#137311 - 11/12/05 07:37 AM Re: Change this to regex?
genius_at_work Offline
Hoopy frood

Registered: 08/10/05
Posts: 1741
This code will only return the first smiley:

Code:
on $*:TEXT:/([-0Oox^*@u'&gt;&lt;])([_.])([-0Oox^*@u'&gt;&lt;])/Si:#:msg $chan $+($regml(3),$regml(2),$regml(1))


The following code will replace all smileys in the line. The optional RED line will include all the other words that the person said. If you don't want the other words, just delete the red line.
Code:
on *:TEXT:*:#:{
  tokenize 32 $strip($1-)
  var %t,%m,%c = 1
  while (%c &lt;= $0) {
    if ($regex(,$($+($,%c),2),/^([-0Oox^*@u'&gt;&lt;])([_.])([-0Oox^*@u'&gt;&lt;])$/i)) {
      %m = %m $+($regml(3),$regml(2),$regml(1))
    }
    [color:red]else %m = %m $($+($,%c),2)[/color]
    inc %c
  }
  if (%m) msg $chan %m
}


-genius_at_work

Top
#137312 - 11/12/05 09:40 AM Re: Change this to regex?
schaefer31 Offline
Fjord artisan

Registered: 27/08/05
Posts: 525
Yep, I wanted it to just msg back the smiley(ies) and not the other text.

It's nice to see two different ways of doing this. I tried both and they worked as requested.

Thanks to both of you.

Top
#137313 - 11/12/05 09:49 AM Re: Change this to regex?
FiberOPtics Offline
Hoopy frood

Registered: 05/02/04
Posts: 2019
Loc: Leuven, Belgium
In my version it would look like:

on *:text:*:#channel: msg # $$smiley($1-).del

If you want to ignore control codes when doing the matching, put the S modifier in the $regsub so that it looks like /Sxg instead of /xg
_________________________
Gone.

Top
#137314 - 11/12/05 09:54 AM Re: Change this to regex?
schaefer31 Offline
Fjord artisan

Registered: 27/08/05
Posts: 525
Yep, I figured that out. I ended up using your code, however I'll hang on to genius's for reference later.

Top