mIRC Home    About    Download    Register    News    Help

Print Thread
Passing an entire line into a $regex/on TEXT call #242865 12/09/13 01:47 AM
Joined: Sep 2013
Posts: 3
0
00000100 Offline OP
Self-satisified door
OP Offline
Self-satisified door
0
Joined: Sep 2013
Posts: 3
I've been wracking my brain on this for weeks, googling, poking around on this board, etc., and never thought to just POST MY OWN QUESTION HERE. Whoops x_______x

EDIT: Ah, shoot...the unicode character that isn't rendering correctly (showing up as Σ) is the sigma character. As the first script is working correctly, I do not believe that the presence of such a unicode character is what is messing up the script.

I am a moderator of a channel on twitch.tv called saltybet. People gamble fake money on AI vs AI fighting games. People can see everyone else's bets, and folks commonly spam the channel with other peoples' bets in the format of:
alphanumericUsernameHere -> $123 (Σ$123)

I am trying to write a script to give a user a temporary timeout if one posts a bet three or more times in sixty seconds. As of now, I have this script working:
Code:
on *:TEXT:* -> $* (Σ$*)*:#saltybet:{
  inc -u60 %betSpam. [ $+ [ $nick ] ] 1
  if (%betSpam. [ $+ [ $nick ] ] >= 3) {
    timer 1 1 /msg $chan .timeout $nick
  }
}


The increment call and variable are working as intended. However, sometimes users post things like this:
alphanumericUsernameHere -> $123 (Σ$123)alphanumericUsernameHere -> $123 (Σ$123)alphanumericUsernameHere -> $123 (Σ$123)alphanumericUsernameHere -> $123 (Σ$123)

In this instance, I would want to use a $regex call to increment the aforementioned %betspam.usernameGoesHere a number of times equal to the number of times the bet appears. Is this making sense?

Here is the modified script that I've been testing using $regex:
Code:
on *:TEXT:* -> $* (Σ$*)*:#test:{
  inc -u60 %testSpam. [ $+ [ $nick ] ] $regex($1-, [A-Za-z0-9]. -> \$[0-9]+ \(Σ\$[0-9]+\))
  msg #test %testSpam. [ $+ [ $nick ] ]
}


The problem that I'm running into is that no matter how many instances of the string appears, the variable only ever increments by one, and when it is messaged back to the channel, it appears as one higher than whatever it was previously. I believe the issue lies in the $1- parameter of the $regex call. I'm kind of hazy on what exactly $1- is, but I thought it was the whole line related to whatever event is currently happening (in this case, maybe the text that met the on:TEXT call?). I've done tests with $1- and it will pass the entire line (with multiple instances of my trigger string) in msg calls.

Can anyone help me figure this out?

P.S.: one other, far simpler question I had is about using an on:TEXT action that will trigger on /me commands as well (so, ideally, on:TEXT||ACTION, if such a thing exists, which it doesn't seem to). Is such a thing possible? Also, should I have made a separate post for this? Nothing in the rules mentioned raising multiple questions in one post, apologies if I should have!

Thanks folks.

Last edited by 00000100; 12/09/13 01:50 AM.
Re: Passing an entire line into a $regex/on TEXT call [Re: 00000100] #242866 12/09/13 04:22 AM
Joined: Jan 2004
Posts: 1,273
L
Loki12583 Online
Hoopy frood
Online
Hoopy frood
L
Joined: Jan 2004
Posts: 1,273
$regex returns 1 for matching, 0 for not matching. If you want the total number of matches you need to use the //g modifier. $regml(0) will then return the number of matches. Use the i and S modifiers for case insensitivity and color stripping. This is now the regex:

/([a-z0-9]+ -> \$\d+ \(Σ\$\d+\))/giS

If you want to catch text and actions have them each call an alias.

Code:
on *:TEXT:* -> $* (Σ$*)*:#test:{
  testspam $1-
}

on *:ACTION:* -> $* (Σ$*)*:#test:{
  testspam $1-
}

alias testspam {
  var %regex = /([a-z0-9]+ -> \$\d+ \(Σ\$\d+\))/giS
  if ($regex($1-, %regex) {
    inc -u60 %betSpam. [ $+ [ $nick ] ] $regml(0)
  }
  if (%betSpam. [ $+ [ $nick ] ] >= 3) {
    msg $chan .timeout $nick
  }
}

Re: Passing an entire line into a $regex/on TEXT call [Re: Loki12583] #242867 12/09/13 04:54 AM
Joined: Jul 2006
Posts: 3,530
W
Wims Offline
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 3,530
Quote:
$regml(0) will then return the number of matches.
$regex will actually return the number of matches, $regml(0) returning the number of backrefs grin


Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net
Re: Passing an entire line into a $regex/on TEXT call [Re: Loki12583] #242868 12/09/13 06:03 AM
Joined: Sep 2013
Posts: 3
0
00000100 Offline OP
Self-satisified door
OP Offline
Self-satisified door
0
Joined: Sep 2013
Posts: 3
EUREKA! I figured it out after typing all the stuff below, but figured I'd post it anyway as I am still curious. My issue was that I need the /g flag, and I needed the leading /. With that said, I understand the /g, why is the leading / necessary? What does that do? (EDIT: Also, thanks for the idea about using aliases in two separate calls. Thanks!)


From the help doc:
"$regex([name], text, re)

Returns N, the number of strings in text that matched the regular expression."

With that said, I follow most of your logic, Loki12583, but I just have a few questions:

Are you making the var %regex as the second argument for the $regex call? As in, instead of typing the obfuscated regular expression, you're just typing %regex, yes? If that is the case, why do you have the leading /( and why are you using the +? Assumedly the former is to denote that all contained within the () is the variable's value, but what purpose does the leading slash serve? Also, is the + is to concatenate? Is that necessary?

Last edited by 00000100; 12/09/13 06:07 AM.
Re: Passing an entire line into a $regex/on TEXT call [Re: 00000100] #242869 12/09/13 09:27 AM
Joined: Aug 2013
Posts: 73
Iire Offline
Babel fish
Offline
Babel fish
Joined: Aug 2013
Posts: 73
The + in regex is actually part of a subset of special characters (metacharacters) called quantifiers; in particular, it matches the preceding patterną if it occurs one or more times in succession... eg: /(asdf)+/i applied to asdfasdfasdfg will match "asdfasdfasdf"

If you're curious about the other quantifiers, click the button below:
Click to reveal..
  • ? - Matches preceding pattern 0 or 1 time (essentially making it optional)... eg: /colou?r/i will match both color and colour
     
  • * - Matches preceding pattern 0 or more times - eg: /yeah*/i will match yea, yeah, yeahhhh, etc.
     
  • {m}/{m,}/{m,n} - Matches the preceding pattern either: exactly m times; m times or more; or, at least m times but no more than n times... eg: /\d{3}/ will match exactly three digits, /d{3,}/ will match three or more digits, and /\d{3,7}/ will match at least three, but no more than seven digits.


It's important to note that quantifiers do not serve as a replacement for global matching (//g), as their reach is limited to the pattern they are quantifying... For example, if you tried to apply the same regex from above - /(asdf)+/i to asdfhelloasdfasdf, only the asdf before hello would be matched since hello breaks the pattern.

1. I hope my use of the term "pattern" here is clear, but of not, i can elaborate.

As for your other question, the // serve as delimiters in the regex to separate the match pattern from modifiers. As for why the first / is necesary when the second is used, I'm not entirely sure; depending on the language, different... let's just call them "premodifiers" - can be specified before the first / to change some function of the regex... The only one I know of in mIRC is "m", which allows you to set the delimiter... eg: m~pattern~modifiers

I asked on IRC about why the leading delimter is needed otherwise, because it's a good question, and while I don't remember the exact details, I was told that it's a syntax borrowed from other languages and is somewhat akin to the quotes of "some string" or 'some string'. (I'll try to provide a more detailed explanation tomorrow.)

For general regex understanding, a resource I reccomend is regular-expressions.info - Not everything on it applies to mirc, but it's still a good guide.

Re: Passing an entire line into a $regex/on TEXT call [Re: Iire] #242871 12/09/13 04:09 PM
Joined: Aug 2013
Posts: 73
Iire Offline
Babel fish
Offline
Babel fish
Joined: Aug 2013
Posts: 73
* /d{3,}/ should be /\d{3,}/

* 1. I hope my use of the term "pattern" here is clear, but if not, i can elaborate.

Re: Passing an entire line into a $regex/on TEXT call [Re: Iire] #242874 13/09/13 12:06 AM
Joined: Sep 2013
Posts: 3
0
00000100 Offline OP
Self-satisified door
OP Offline
Self-satisified door
0
Joined: Sep 2013
Posts: 3
Hi lire, thanks for the response.

Duh! I feel dumb, I knew that + was one or more occurances. Total brainfart moment. Just to confirm, using [a-c]+ doesn't have to be the SAME character, it just means anything from the specified range, yes?

I understand what you mean by pattern, yes. Also, thanks for looking into the leading / for me! I appreciate your teaching.

Re: Passing an entire line into a $regex/on TEXT call [Re: 00000100] #242875 13/09/13 05:58 AM
Joined: Aug 2013
Posts: 73
Iire Offline
Babel fish
Offline
Babel fish
Joined: Aug 2013
Posts: 73
Originally Posted By: 00000100
Just to confirm, using [a-c]+ doesn't have to be the SAME character, it just means anything from the specified range, yes?


Yes, that's correct.