AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

RegEx Callbacks

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
majkinetor



Joined: 24 May 2006
Posts: 4511
Location: Belgrade

PostPosted: Fri Jan 09, 2009 2:05 pm    Post subject: RegEx Callbacks Reply with quote

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)"
}

_________________
Back to top
View user's profile Send private message
kiropes



Joined: 21 Mar 2007
Posts: 71

PostPosted: Sat Jan 10, 2009 2:21 pm    Post subject: Reply with quote

the links don't work
Back to top
View user's profile Send private message
freakkk



Joined: 29 Jul 2005
Posts: 179

PostPosted: Sat Jan 10, 2009 2:23 pm    Post subject: Reply with quote

kiropes wrote:
the links don't work
Yes they do
_________________
.o0[ corey ]0o.
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4710
Location: Boulder, CO

PostPosted: Sat Jan 10, 2009 4:21 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
haichen



Joined: 05 Feb 2007
Posts: 189
Location: Osnabrück, Germany

PostPosted: Sat Jan 10, 2009 7:19 pm    Post subject: Reply with quote

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)"
}
Back to top
View user's profile Send private message
majkinetor



Joined: 24 May 2006
Posts: 4511
Location: Belgrade

PostPosted: Sun Jan 11, 2009 1:26 am    Post subject: Reply with quote

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.
_________________
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group