mIRC Home    About    Download    Register    News    Help

Print Thread
#190243 18/11/07 06:42 AM
Joined: Oct 2007
Posts: 92
N
Babel fish
OP Offline
Babel fish
N
Joined: Oct 2007
Posts: 92
is it possible to use regex to match
if %nicks == nick1,nick2,nick2

Last edited by nomer2007; 18/11/07 06:59 AM.
nomer2007 #190247 18/11/07 08:39 AM
Joined: Dec 2002
Posts: 503
B
Fjord artisan
Offline
Fjord artisan
B
Joined: Dec 2002
Posts: 503
You just want $istok().
Code:
var %nicks = nick1,nick2,nick3
if ($istok(%nicks, $nick, 44)) { 
  echo -a yup
}

Bekar #190248 18/11/07 08:51 AM
Joined: Oct 2007
Posts: 92
N
Babel fish
OP Offline
Babel fish
N
Joined: Oct 2007
Posts: 92
yes i know about $istok my question is can it be done like the istok is doing but using $regex ?

nomer2007 #190249 18/11/07 09:52 AM
Joined: Dec 2002
Posts: 503
B
Fjord artisan
Offline
Fjord artisan
B
Joined: Dec 2002
Posts: 503
Err, yes.. But why? Overkill.

Bekar #190250 18/11/07 10:39 AM
Joined: Oct 2007
Posts: 92
N
Babel fish
OP Offline
Babel fish
N
Joined: Oct 2007
Posts: 92
i know about using tokens but not regex so i'm trying to ask cause i want to learn.. grin

nomer2007 #190252 18/11/07 11:43 AM
Joined: Dec 2002
Posts: 503
B
Fjord artisan
Offline
Fjord artisan
B
Joined: Dec 2002
Posts: 503
Righteo then!

Here's a few pointers to get you started:

Code:
var %string = This is a long string of text
if (lon isincs %string) {
  echo -a yup
}
-- 
if ($regex(%string, /lon/)) {
  echo -a yup
}

These lines are roughly equivalent.

At it's most basic, the $regex() matching will do partial string matches that are case sensitive. You can make it case insensitive if you want by adding an 'i' after the pattern markers (/lon/i).

Now, to do full word matching like an $istok(), we need to be a little more complex:

Code:
var %string = nick1|nick2|nick3
var %nick = nick2
if ($istok(%string, %nick, 124)) {
  echo -a yup
}
--
if ($regex(%nick, /^( $+ %string $+ )$/i)) {
  echo -a yup
}

This is as close as I could get to a simple $istok().

We have to use the pipe symbol (|) as the separator for the regular expression, because we're basically saying "do you mach nick1 to this string, or that string, or the other string". The pipe is a regex 'atom' separator, and it's the only thing that can be used for it.

Now, that being said, we also only want to match the whole string, rather than just a partial string like we did above. That's what the ^ (carot) and $ (dollar) symbols are. They're termed 'anchor'. The carot is the start-of-string anchor, whilst the dollar is the end-of-string anchor.

The long way of writing the pattern would be something like:

Code:
/^nick1$|^nick2$|^nick3$/i

But as you can see, it's fairly repetitive. As the start-end anchors are the same for all the middle bits, using parenthesis (()) allows you to 'group' sub patterns to wrap it with other tricky bits.

This ability to group sub patterns is one of regular expressions strong points.

Now, all of this time, we're only doing simple string matches. We're not manipulating ($regsub()/$regsubex()) or even pulling out the matched patterns ($regml()). Using the grouping parenthesis with these functions, and you can see this becoming a monstrous tool.

Yay.. wink

Have a quick poke through these forums, there will be links to various tutorials, or just google 'regular expression tutorial mirc', or 'perlre' (which is the regular expression library that mIRC uses). If you get stuck (you probably will), feel free to poke us.. smile There are a few guys 'n girls on these forums who are very good with their regexp's smile


Bekar #190268 18/11/07 03:15 PM
Joined: Oct 2007
Posts: 92
N
Babel fish
OP Offline
Babel fish
N
Joined: Oct 2007
Posts: 92
Thank you for the good reply sir this is very helpful

Code:
;;set to %string
set %string = nick1|nick2|nick3
--
on *:join:#: {
  if ($regex($nick, /^( $+ %string $+ )$/i)) {
    echo -a yup
  }
}


but this didn't worked

$nick == nick1

Last edited by nomer2007; 18/11/07 03:36 PM.
nomer2007 #190278 18/11/07 05:41 PM
Joined: Nov 2006
Posts: 1,559
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Nov 2006
Posts: 1,559
The example is valid if you remove the = char in the set command - and it should work in the majority of cases smile But it's no "secure" method: think of nicks like "nick1|away" or "[^a-z]" etc...

It could be debugged (e.g. by escaping meta-chars via replace, or by using quote), but why do you want to make it catchier than needed?
You have a string of delimited tokens and want to return a match? Use the token identifiers smile


Link Copied to Clipboard