mIRC Homepage
Posted By: DJ_Sol return matched with regex - 08/08/07 07:29 PM
Hi, Im trying to figure out how to use regex. I have a long line of characters and I want to return the data found in between the first set of [ ].

I have been looking and looking for tutorials to help me. Can someone help me with this or at least show me where I can read how to do it? Thank you!
Posted By: sparta Re: return matched with regex - 08/08/07 07:39 PM
Here i find the most info if i try something new, or i ask in the forum here, how ever the site looks to be down for the moment, but you can try connect to it later on:

http://script.quakenet.org/wiki/Main_Page

You can also look at: http://www.mircscripts.org/
Posted By: SladeKraven Re: return matched with regex - 08/08/07 07:53 PM
Not too sure with regex, never really learnt it. But for now it can be done with tokens.

Code:
//var %data = This is a test [This is not a test] This is a test | echo 2 -a $gettok($gettok(%data,2,91),1,93) | echo -a %data
Posted By: DJ_Sol Re: return matched with regex - 08/08/07 09:38 PM
yeah Im using tokens right now, but would prefer using regex. Thanks smile
Posted By: hixxy Re: return matched with regex - 08/08/07 10:03 PM
Code:
alias regextest {
  var %text = this [is a long] string
  var %re = /(\[.*?\])/
  noop $regex(%text,%re)
  echo -a $regml(1)
}


I'll try and break it down:

/(\[.*?\])/

These are just regex delimiters. You should always enclose your regex inside of these.
When you capture a pattern inside of () brackets, you can reference it later with \1 or $regml(1)
You match \[ rather than just [ because the [ and ] characters have special meaning in regular expressions, and \ acts as an escape character.
. means any one character. * is a quantifier and means '0 or more', so when applied to ., .* join together and this expression becomes '0 or more cases of 1 character'. I wouldn't worry about the ? for now until you've got a basic grasp of simpler concepts.
Posted By: DJ_Sol Re: return matched with regex - 08/08/07 11:20 PM
Thank you very much. It made sense. Im going to put it into practice. Cheers!
© mIRC Discussion Forums