Basic $regex example

By KuTsuM on Dec 31, 2005

This is a basic talker bot using the $regex alias for those who want to get started in Regular Expressions. There are a few triggers (These aren't case sensetive, punctuation won't affect it, and you can sort of mix it with other words):

Hey/Hi/Hello -
My name is -
(I'm or I am) years old -
lol (occassional trigger, 5-9 chance of triggering) -

I know this description may be kind of confusing, but just put this code in your bot, log onto another nickname, and type some of the above into the channel, you'll figure it out.

on *:TEXT:*:#:{
  if ($regex($1-,(H|h)ello.|(H|h)ey.|(H|h)i. (.+?)\b) > 0) && ($regml(2) == $me) { msg $chan Hello, $+($nick,.) }
  if ($regex($1-,(N|n)ame.is (.+)) > 0) { msg $chan Hello, $+($regml(2),.) I'm $+($me,$chr(44)) it's nice to meet you. ;) }
  if ($regex($1-,(L|l)(O|o)(L|l)) > 0) {
    if ($rand(0,9) < 4) { msg $chan LOL! Pretty funny stuff, $+($nick,!) }
    elseif ($rand(0,9) < 4) { msg $chan It really wasn't that funny, $+($nick,.) }
  }
  if ($regex($1-,^((I|i).m|(I|i).am) (.+) (Y|y)ears.(O|o)ld) > 0) {
    if ($regml(3) < 13) { msg $chan You're kidding, $+($nick,.) Only $v1 years old? | halt }
    if ($regml(3) >= 100) { msg $chan Yeah, right. If you don't want to tell me your age $+([,$nick,]), just tell me. You don't have to lie about it! Pff, $regml(3) years old, right. | halt }
    msg $chan Cool, $+($nick,.) I, for some reason, wish I was $+($regml(3),!)
  }
}

Comments

Sign in to comment.
Night-Sky   -  Sep 22, 2012

awesome kutsum tnx to ur liltle example i managed to make a pretty script for a bot :)

tnx a lot

 Respond  
KuTsuM   -  Jan 09, 2006

The ?: I never knew about. I have never come across that any tutorials or from messing around with it, As I said to Yoinx \"Yoinx, I guess I just learned something new ;p.\" As for the digit, I know 1 and 0 represent a \"true/false\" value in a way, but it\'s a habit of mine that, as if someone didn\'t know I have OCD, bothers me if I don\'t follow through with it ;/ I\'ll try to quit, lol. Anyways, thanks for this tips FiberOptics ;p

 Respond  
FiberOPtics   -  Jan 04, 2006

Btw, on another note:

$regex always returns a digit in range 0-, in other words, you don\'t need to check for > 0.

In an if statement, when checking a value like: if (value)

the condition evaluates to $true when the value is not 0, not $false, and not $null. In the same way, the if statement returns $false, if the value was either 0, $false, or $null.

Since $regex is always a digit, we can already rule out the $true and $false, leaving us with only the digit 0 to make the if condition false.

Therefore \"if ($regex([name],value,))\" is sufficient.

 Respond  
FiberOPtics   -  Jan 04, 2006

You should always put your expressions between slashes / /
before you run into this situation:

//echo -a $regex(a,moo)

and wonder why it says there is 1 match...

Also, when you create regexes that capture a pattern, it\'s also a good idea to name your regexes, because everytime a regex captures, it overwrites the old results in $regml.

In cases where you are not planning to reference the captured results later with $regml, it is more efficient to create an uncapturing expression:

(?:L|l) instead of (L|l)

but anyway you should just use the i modifier which will make your expressoin case insensitive:

From: if ($regex($1-,(L|l)(O|o)(L|l)) > 0) {

to

if ($regex($1-,/lol/i)) {

The i modifier tells the regex engine that the specified alpha chars are case insensitive.

I could go on and on and give tips for improvement, but that would end up being a tutorial, something that already exists.

 Respond  
KuTsuM   -  Jan 03, 2006

The >= 4 is to set certain chances (If it answers to the same common thing all the time, it\'s really annoying), and as for the hey/hi/hello, you can choose between the 3. So it\'d be Hello, . or Hey, .. your choice

 Respond  
Corne   -  Jan 02, 2006

The elseif to $rand, shouldnt be elseif ($rand(0,9) >= 4) .... ? I red your notes but then it should be 0,1,2,3 (4 changes to trigger)
I dont know much about regex but the hi/hey/hello regex doesnt work for me. The bot should answer to \'hi/hello/hey \' right ? ohh well, it doesnt

 Respond  
DarthReven   -  Jan 02, 2006

Instead of using \"if ($regex($1-,(L|l)(O|o)(L|l)) > 0)\" Use \"if ($regex($1-,(L|l)(O|o)(L|l)))\" its much cleaner and does the same thing

 Respond  
Yoinx   -  Jan 01, 2006

I could be wrong. Im not good with regex at all... Have you tried sigh\'s rbuddy snippet.... nice....

 Respond  
KuTsuM   -  Jan 01, 2006

@Rapid, I know it\'s easier, but this was to help teach $regex, not to make a script. That\'s the point of snippets.
Yoinx, I guess I just learned something new ;p

 Respond  
Yoinx   -  Jan 01, 2006

(:?H|h)ey im pretty sure that stops it from backreferencing that group kutsum. So that instead of using $regml(2) for what you\'re looking for, you can just use 1. no biggie though I guess.

 Respond  
Noutrious   -  Jan 01, 2006

Oh god..
20x easier is to use if (hello isin $1-) or if (hello == $1)

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.