| View previous topic :: View next topic |
| Author |
Message |
John B.
Joined: 21 Oct 2006 Posts: 21
|
Posted: Wed Dec 12, 2007 12:39 am Post subject: Could Loop, Filepattern use regular expressions? |
|
|
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:
matches a.hhc AND a.hhc_backup
But if the Filepattern could be a regular expression, then I could specify the pattern as
to specifically match just the file extension I want.
Thanks,
John B.
(who just loves regular expressoins) |
|
| Back to top |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 1886 Location: Germany
|
Posted: Wed Dec 12, 2007 11:13 pm Post subject: |
|
|
| This would break existing scripts and also slow down if regex is not needed. |
|
| Back to top |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 8255 Location: Maywood, IL
|
Posted: Mon Dec 17, 2007 4:47 pm Post subject: |
|
|
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 |
|
 |
polyethene
Joined: 11 Aug 2004 Posts: 5248 Location: UK
|
Posted: Mon Dec 17, 2007 5:05 pm Post subject: |
|
|
I like engineers idea, an regexmatch equality operator would be fantastic! Something similar to perl, e.g. if ("abcdef" ~= "\w{1,10}") == true _________________ GitHub • Scripts • IronAHK • Contact by email not private message. |
|
| Back to top |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 1886 Location: Germany
|
Posted: Mon Dec 17, 2007 7:22 pm Post subject: |
|
|
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 |
|
 |
engunneer
Joined: 30 Aug 2005 Posts: 8255 Location: Maywood, IL
|
Posted: Mon Dec 17, 2007 7:25 pm Post subject: |
|
|
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 |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 1886 Location: Germany
|
Posted: Fri Aug 21, 2009 7:16 am Post subject: |
|
|
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 |
|
 |
majkinetor
Joined: 24 May 2006 Posts: 4511 Location: Belgrade
|
Posted: Sun Aug 23, 2009 6:34 pm Post subject: |
|
|
++
RegEx everywhere FTW. _________________
 |
|
| Back to top |
|
 |
HotKeyIt
Joined: 18 Jun 2008 Posts: 4652 Location: AHK Forum
|
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 3254 Location: Simi Valley, CA
|
Posted: Sun Aug 23, 2009 8:24 pm Post subject: |
|
|
^ 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 |
|
 |
HotKeyIt
Joined: 18 Jun 2008 Posts: 4652 Location: AHK Forum
|
Posted: Sun Aug 23, 2009 9:08 pm Post subject: |
|
|
| [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  |
|
| Back to top |
|
 |
[VxE]
Joined: 07 Oct 2006 Posts: 3254 Location: Simi Valley, CA
|
Posted: Mon Aug 24, 2009 2:13 am Post subject: |
|
|
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 |
|
 |
|