AutoHotkey Community

It is currently May 26th, 2012, 1:59 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: RegEx Callbacks
PostPosted: January 9th, 2009, 3:05 pm 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
From recently, there are some efforts to introduce callouts to RegEx engine.
Before it gets done, this module can be used to achieve something similar.


Download        Documentation


Example:
Replace the server names with base name and ping information:

Code:
   text=
   (Ltrim
      www.google.com
      www.yahoo.com
      www.autohotkey.com
   )
   msgbox % RegEx_Replace(text, "www\.(.+?)\.com", "MyFunc")
return

MyFunc($, $1) {
   RunWait, cmd /C ping %$% -n 1 >out, ,Hide
   FileRead out, out
   RegExMatch(out, "Reply from .+? time=([0-9]+)ms", o)
   return $1 " (" o1 " ms)"
}

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 10th, 2009, 3:21 pm 
Offline

Joined: March 21st, 2007, 7:50 pm
Posts: 76
the links don't work


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 10th, 2009, 3:23 pm 
Offline

Joined: July 29th, 2005, 5:32 pm
Posts: 179
kiropes wrote:
the links don't work
Yes they do

_________________
.o0[ corey ]0o.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 10th, 2009, 5:21 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Interesting! Such short AHK code, with great functionality!
- Is it basically the same as replacing all matches with special strings, and then use a Parse loop to process those strings?
- Can this function be used for finding all matches, even within other matches? Like listing all substrings of a 4 letter haystack: abcd --> a, b, c, d, ab, bc, cd, abc, bcd, abcd.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 10th, 2009, 8:19 pm 
Offline

Joined: February 5th, 2007, 12:19 pm
Posts: 192
Location: Osnabrück, Germany
kiropes wrote:
the links don't work

You probably forget to translate to your local language.

Here's the german version:
Code:
MyFunc($, $1) {
   RunWait, cmd /C ping %$% -n 1 >out, ,Hide
   FileRead out, out
   RegExMatch(out, "Antwort von .+? Zeit=([0-9]+)ms", o)
   return $1 " (" o1 " ms)"
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 11th, 2009, 2:26 am 
Offline

Joined: May 24th, 2006, 2:49 pm
Posts: 4511
Location: Belgrade
Laszlo wrote:
Is it basically the same as replacing all matches with special strings, and then use a Parse loop to process those strings?

I think this allows for broather usage, not to mention far easier.

Laszlo wrote:
- Can this function be used for finding all matches, even within other matches? Like listing all substrings of a 4 letter haystack: abcd --> a, b, c, d, ab, bc, cd, abc, bcd, abcd.

No. The next matching starts after previous one (see the code, you can change that line for other effects if you want). I don't think you can do that with integrated RegExp functions, right ?

The only note you should be aware so far is that you can't access named sub-patterns in callback function. You can name function arguments the way you want so there is no need to use them, generally.

_________________
Image


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: c0ntinuity, Yahoo [Bot] and 15 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group