mIRC Homepage
Posted By: vexed2 regex question/fix - 20/10/08 06:55 PM
I'm using the piece of code underneath to log and load urls, ftp links and such like into a simple dialog.
The problem i have just picked up on, It'll only log the first link, and not the rest if they are more on 1 line, would be greatful for a fix for this. Thanks.

Code:
on ^1:TEXT:*:?:{
  var %input = $strip($1-)
  var %url $regml($regex(%text,/((?:ftp:\/\/|https?:\/\/|www2?\.)[^<>\.\s]+(?:\.[^<>\.\s]+)+(?:\/[^<>\.\s]+)*)/g))
  if ($regml($regex(%text,/((?:ftp:\/\/|https?:\/\/|www2?\.)[^<>\.\s]+(?:\.[^<>\.\s]+)+(?:\/[^<>\.\s]+)*)/g))) {
    if ($read(system\url.txt,w,$regml(1)) == $null) { write system\url.txt $regml(1) }
    if ($dialog(url)) { loadbuf -ro url 1 system\url.txt }
    haltdef
  }
}
Posted By: Mpdreamz Re: regex question/fix - 21/10/08 11:15 AM
Loop through $regml(0) to get all the matches
Posted By: Albertrud Re: regex question/fix - 21/10/08 03:33 PM
What you're doing here, every time you pass $regex(%text,....) it is filling the $regml(n) with all the urls in the text.
$regex() it self will return the total number of matches, if this is 1, 2, 3 or 4 or what ever it may be, you'll do $regml(1) (or the total amount of matches)

What you need to do, is simple store the amount of matches to a variable and loop through all $regml(n) until you've been through the list.

Also worth a note you could make sure you're likely to want to try and capture the text inside your text event and use the 'strip' modifier as below.

Code:
on ^*:text:/ftp|http|www/iS:?: {
  var %i = $regex(%text,/((?:ftp:\/\/|https?:\/\/|www2?\.)[^<>\.\s]+(?:\.[^<>\.\s]+)+(?:\/[^<>\.\s]+)*)/giS)
  while (%i) {
    if ($read(system\url.txt,w,$regml(%i) == $null) write system\url.txt $regml(%i)
    dec %i
  }
  if ($dialog(url)) loadbuf -ro url 1 system\url.txt
}
© mIRC Discussion Forums