|
schaefer31
|
schaefer31
|
I have the following 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.
|
|
|
|
Joined: Feb 2004
Posts: 2,013
Hoopy frood
|
Hoopy frood
Joined: Feb 2004
Posts: 2,013 |
/* 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 ».« --> «.» */ alias sm.set return [-0Oox^*@uòóôõöøÒÓÔÕÖØ'°ºùúûüÙÚÛÜ><»«]
alias sm.sep return [_¯.]
alias smiley {
var %a, %b = (?<=\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.
|
|
|
|
Joined: Oct 2005
Posts: 1,671
Hoopy frood
|
Hoopy frood
Joined: Oct 2005
Posts: 1,671 |
This code will only return the first smiley:
on $*:TEXT:/([-0Oox^*@uòóôõöøÒÓÔÕÖØ'°ºùúûüÙÚÛÜ><»«])([_¯.])([-0Oox^*@uòóôõöøÒÓÔÕÖØ'°ºùúûüÙÚÛÜ><»«])/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.
on *:TEXT:*:#:{
tokenize 32 $strip($1-)
var %t,%m,%c = 1
while (%c <= $0) {
if ($regex(,$($+($,%c),2),/^([-0Oox^*@uòóôõöøÒÓÔÕÖØ'°ºùúûüÙÚÛÜ><»«])([_¯.])([-0Oox^*@uòóôõöøÒÓÔÕÖØ'°ºùúûüÙÚÛÜ><»«])$/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
|
|
|
|
schaefer31
|
schaefer31
|
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.
|
|
|
|
Joined: Feb 2004
Posts: 2,013
Hoopy frood
|
Hoopy frood
Joined: Feb 2004
Posts: 2,013 |
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
|
|
|
|
schaefer31
|
schaefer31
|
Yep, I figured that out. I ended up using your code, however I'll hang on to genius's for reference later.
|
|
|
|
|