mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Dec 2002
Posts: 122
S
STING Offline OP
Vogon poet
OP Offline
Vogon poet
S
Joined: Dec 2002
Posts: 122
So after many years, I'm finally learning myself regular expressions wink

My first attempt should be a simple enough task, getting an event to trigger on your nickname, while not being part of another word (STING=true, teSTING=false)

So far I succeed with a '==' and 3 $regex'es, but I think this could probably be done by one $regex, right? smile


Code:
alias _regex {

  ;should be False
  var %text.1 = testing 

  ;should be True
  var %text.2 = test STING test 

  ;should be True
  var %text.3 = STING test 

  ;should be True
  var %text.4 = test STING 

  ;should be True
  var %text.5 = STING 

  var %tmp = 0
  clear
  while (%tmp < 5) {
    inc %tmp
    var -s %txt = %text. [ $+ [ %tmp ] ]
    if (%txt == STING) || $regex(%txt,/ STING /) || $regex(%txt,STING$) || $regex(%txt,^STING) { echo -a % $+ text. $+ %tmp is 3True }
    else { echo -a % $+ text. $+ %tmp is 4False }
  }
}


Joined: Feb 2006
Posts: 546
J
Fjord artisan
Offline
Fjord artisan
J
Joined: Feb 2006
Posts: 546
good on ya STING :P the reason 'teSTING' is not being matched is that regex in mIRC is, by default, case sensitive. if you use the pattern /STING$/i on that second last check, 'teSTING' would then be matched. if you mean to combine the following:

/^STING$/
/^STING /
/ STING$/
/ STING /

ie. the regex equivalent of checking $istok( ,STING,32), then you should take note of the fact that they are all quite similar and include your nick somewhere in the middle. more precisely, on the left side of your nickname we want to match either ^ or a space; on the right side of your nickname we want to match either a space or $. we can then use | (indicating alternatives, you can think of it as an 'OR' operator) combined with ( ) (a group) to produce:

/(^| )STING( |$)/

( ) also have the property of saving what is matched for later reference with $regml(). quite often this is wasteful and unnecessary, (?: ) can be used to group alternatives without saving the result. you'll come across these constructs later on i imagine, good luck!


"The only excuse for making a useless script is that one admires it intensely" - Oscar Wilde
Joined: Jul 2007
Posts: 1,129
T
Hoopy frood
Offline
Hoopy frood
T
Joined: Jul 2007
Posts: 1,129
/(?:sting)/ does not alter the matching behavior, the regex just doesn't capture it.

Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
That's what he said, "(?: ) can be used to group alternatives without saving the result."

Joined: Dec 2002
Posts: 122
S
STING Offline OP
Vogon poet
OP Offline
Vogon poet
S
Joined: Dec 2002
Posts: 122
Thanks guys wink


Link Copied to Clipboard