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 

Could Loop, Filepattern use regular expressions?

 
Reply to topic    AutoHotkey Community Forum Index -> Wish List
View previous topic :: View next topic  
Author Message
John B.



Joined: 21 Oct 2006
Posts: 21

PostPosted: Wed Dec 12, 2007 12:39 am    Post subject: Could Loop, Filepattern use regular expressions? Reply with quote

Hi,

I posted a question over in Ask for Help, but it's led me to make a suggestion as well. The problem I encountered is that the Filepattern in a file loop can match a string anywhere in the file name. Thus:
Code:
Loop, *.hhc

matches a.hhc AND a.hhc_backup

But if the Filepattern could be a regular expression, then I could specify the pattern as
Code:
Loop, *\.hhc$

to specifically match just the file extension I want.

Thanks,
John B.
(who just loves regular expressoins)
Back to top
View user's profile Send private message
Tuncay



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Wed Dec 12, 2007 11:13 pm    Post subject: Reply with quote

This would break existing scripts and also slow down if regex is not needed.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
engunneer



Joined: 30 Aug 2005
Posts: 8255
Location: Maywood, IL

PostPosted: Mon Dec 17, 2007 4:47 pm    Post subject: Reply with quote

perhaps we could add then SetLoopMatchMode as in SetTitleMatchMode, which has a regex option.

This would make a powerful grep-like tool.
_________________

(Common Answers)
Back to top
View user's profile Send private message Visit poster's website
polyethene



Joined: 11 Aug 2004
Posts: 5248
Location: UK

PostPosted: Mon Dec 17, 2007 5:05 pm    Post subject: Reply with quote

I like engineers idea, an regexmatch equality operator would be fantastic! Something similar to perl, e.g. if ("abcdef" ~= "\w{1,10}") == true
_________________
GitHubScriptsIronAHK Contact by email not private message.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Tuncay



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Mon Dec 17, 2007 7:22 pm    Post subject: Reply with quote

A regex match operator??? Wow, sounds very good idea to me, I would find it also very useful.
If (var ~= var2)
I like that syntax.

Back again to topic, the SetLoopMatchMode option seems to be a simple solution to me. But, is this really needfull? First, we should answer this question.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
engunneer



Joined: 30 Aug 2005
Posts: 8255
Location: Maywood, IL

PostPosted: Mon Dec 17, 2007 7:25 pm    Post subject: Reply with quote

It would be extremely powerful

The equivalent would be to
Code:

Loop, *.*
{
  if not regexmatch(A_LoopFileFullPath, "REGEX")
    continue
  ;rest of loop here
}

_________________

(Common Answers)
Back to top
View user's profile Send private message Visit poster's website
Tuncay



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Fri Aug 21, 2009 7:16 am    Post subject: Reply with quote

We often need regex. Instead to create new operator or special coded support of regex at some functions, I wish we could specifiy every (expression-) string or variable as a regex. That is more natural and usable in more situations. In example with a special symbol or character (example the ~ symbol) AutoHotkey knows the next string is a regex. This allows us in general to write faster regex. Of course we cannot use in that case all features, i.e. the parameters. But it could lead also to faster code, because there is less to parse. And ErrorLevel could be automatically set to last match.

Code:
If (var = ~"??\.??")
    MsgBox %ErrorLevel%

Loop, ~%var%
    MsgBox %A_LoopFileFullPath%


Last edited by Tuncay on Sun Aug 23, 2009 6:40 pm; edited 1 time in total
Back to top
View user's profile Send private message Send e-mail Visit poster's website
majkinetor



Joined: 24 May 2006
Posts: 4511
Location: Belgrade

PostPosted: Sun Aug 23, 2009 6:34 pm    Post subject: Reply with quote

++

RegEx everywhere FTW.
_________________
Back to top
View user's profile Send private message
HotKeyIt



Joined: 18 Jun 2008
Posts: 4652
Location: AHK Forum

PostPosted: Sun Aug 23, 2009 7:21 pm    Post subject: Reply with quote

As far as I understand this is not possible because it is not supported by FindFirstFile Function Sad

MSDN FindFirstFile Function wrote:
...
Parameters

lpFileName [in]
    The directory or path, and the file name, which can include wildcard characters, for example, an asterisk (*) or a question mark (?).
...

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
[VxE]



Joined: 07 Oct 2006
Posts: 3254
Location: Simi Valley, CA

PostPosted: Sun Aug 23, 2009 8:24 pm    Post subject: Reply with quote

^ That's hardly an obstacle; as Engunneer pointed out, a "Loop, FilePatternRegex" could check each filepath against the needle. Such a loop might instead create a list of files, filter them with the needle, and loop through the filtered list.

As far as syntax goes, I think something along the lines of Loop, FileRegex, NeedleRegex, [include folders, recurse] would make more sense than a SetLoopMatchMode, ... and it would be consistent with the syntax for other special loops.
_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!
Back to top
View user's profile Send private message
HotKeyIt



Joined: 18 Jun 2008
Posts: 4652
Location: AHK Forum

PostPosted: Sun Aug 23, 2009 9:08 pm    Post subject: Reply with quote

[VxE] wrote:
^ That's hardly an obstacle; as Engunneer pointed out, a "Loop, FilePatternRegex" could check each filepath against the needle. Such a loop might instead create a list of files, filter them with the needle, and loop through the filtered list.


This would be similar to this and would be not much faster as I understand:
Code:
Loop, C:\*,1,1
 If RegExMatch(A_LoopFileLongPath,"^.*\.ahk$")
  MsgBox % A_LoopFileName

_________________
AHK_H (2alpha) AHF TT _Struct WatchDir Yaml _Input ObjTree RapidHotkey DynaRun Wink
Back to top
View user's profile Send private message
[VxE]



Joined: 07 Oct 2006
Posts: 3254
Location: Simi Valley, CA

PostPosted: Mon Aug 24, 2009 2:13 am    Post subject: Reply with quote

Heh, if there were no way to specify a 'starting folder' for the loop, then, yes, that would be an accurate illustration.

Anyways, I'm not convinced that there is an elegant, intuitive solution for combining a file loop with regex, however, what good is it to say it's impossible ?

For the time being, using something like
Code:
Loop, %A_MyMusic%\* ; a normal file loop has limited precision regarding wildcards
{
  if not regexmatch(A_LoopFileName, "REGEX") ; A_LoopFileName could instead be any file-loop variable
    continue
  ;rest of loop here
}
is more than sufficient, and it's unlikely to confuse anyone. The OP is merely arguing that he/she would like it to be condensed into its own type of loop.
_________________
Ternary (a ? b : c) guide     TSV Table Manipulation Library
Post code inside [code][/code] tags!
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Wish List 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