mIRC Home    About    Download    Register    News    Help

Print Thread
#182379 08/08/07 07:29 PM
Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
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!

Joined: Feb 2003
Posts: 3,432
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Feb 2003
Posts: 3,432
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/


if ($me != tired) { return } | else { echo -a Get a pot of coffee now $+($me,.) }
Joined: Dec 2002
Posts: 3,547
S
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
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

Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
yeah Im using tokens right now, but would prefer using regex. Thanks smile

Joined: Sep 2005
Posts: 2,881
H
Hoopy frood
Offline
Hoopy frood
H
Joined: Sep 2005
Posts: 2,881
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.

Joined: Jan 2007
Posts: 1,156
D
DJ_Sol Offline OP
Hoopy frood
OP Offline
Hoopy frood
D
Joined: Jan 2007
Posts: 1,156
Thank you very much. It made sense. Im going to put it into practice. Cheers!


Link Copied to Clipboard