mIRC Homepage
Posted By: kocam need help with regular expressions - 06/05/04 07:01 PM
Hi, I am new on this forum.
I'd like to know how to make a $regex with a pattern that checks if:
- first word (required), is !add or !addme;
- second word (optional), if specified, is a or b.

sry 4 my bad english

Thanks smile
Posted By: FiberOPtics Re: need help with regular expressions - 06/05/04 07:10 PM
Hello,

I'm fairly new to regex, so others may have a more optimized regex, though this worx fine:
Code:
  
if $regex($1-,/^!add(?:me)?(?:\s[ab])?$/) { do stuff }


Greetz
Posted By: kocam Re: need help with regular expressions - 06/05/04 07:39 PM
Thanks. grin

I Forgot 2 things :\

- case unsensitive
- regml return for second word

Thanks again smile
Posted By: FiberOPtics Re: need help with regular expressions - 06/05/04 08:10 PM
Hi,

Code:
 
if $regex($1-,/^!add(?:me)?(\s[ab])?$/i) { echo -a $regml(1) | ... }


So as you can see, due to the /i the match is case insensitive, and when the second letter (a or b) is matched, the match will be stored in $regml(1)

So 3 possibilities:

1) regex doesn't match
2) regex matches, but only on !add or !addme, so $regml(1) will be empty as there is no 2nd parameter (a or b) given
3) regex matches, with the 2nd parameter (a or b) which is stored in $regml(1)

Note that i used the echo command simply as an example.


Greetz


Edit: Note that you don't even need a regex to do what you want, although the regex is a little tidier. You could do:
Code:
 
if $istok(!add !addme,$lower($1),32) {
  if $istok(a b,$lower($2),32) { ... }
  else { ... }
}

Well you see where I'm going with this smile

Posted By: kocam Re: need help with regular expressions - 06/05/04 09:32 PM
I know i can make script work without ER but I just want start learning them with some easy tutorials...

Anyway, thank u for the time spent on my problems grin

Posted By: FiberOPtics Re: need help with regular expressions - 06/05/04 09:37 PM
You're welcome!
Posted By: MIMP Re: need help with regular expressions - 06/05/04 10:45 PM
You might want to check out http://www.regular-expressions.info/ for help with learning regular expressions.
Posted By: qwerty Re: need help with regular expressions - 07/05/04 12:59 AM
Note that you don't need to use $lower(), since $*tok() identifiers are not case-sensitive (their case-sensitive counterparts are the $*tokcs() identifiers).
Posted By: FiberOPtics Re: need help with regular expressions - 07/05/04 01:31 AM
Yep,

didn't think of it...but well as he stated he wanted the regex anyway, was merely an example typed out the back of my head.

But right is right smile

So:
Code:

if $istok(!add !addme,$1,32) {
  if $istok(a b,$2,32) { ... }
  else { ... }
}

Greetz

© mIRC Discussion Forums