| View previous topic :: View next topic |
| Author |
Message |
sinkfaze
Joined: 18 Mar 2008 Posts: 2428
|
Posted: Sat Apr 25, 2009 11:34 pm Post subject: |
|
|
| rulfzid wrote: | @Sinkfaze
I wonder what causes the differences in performance between your regex and mine. I'd guess it's using the named subpatterns? |
My guess is it's your look-ahead assertions since they don't consume any characters. I've been trying to find a good way to approach using those in my tutorial but I haven't come up with a good enough example. _________________ Try Quick Search for Autohotkey or see the tutorial for newbies. |
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 2428
|
|
| Back to top |
|
 |
animeaime
Joined: 04 Nov 2008 Posts: 1046
|
Posted: Sat Apr 25, 2009 11:36 pm Post subject: |
|
|
| Sean wrote: | In this case, RegEx is not that un-intuitive, really verbatim to what we actually carried out.
| Code: | Folder := "C:\FolderA\FolderB\FolderC\"
;Folder := "C:\FolderA\FolderB\FolderC"
RegExMatch(Folder,"^.*\\(?=[^\\]+\\?$)",Root)
MsgBox % Root
|
|
See what I mean, yal are in a different league... that RegEx makes no sense to me, but it works - somehow  _________________ As always, if you have any further questions, don't hesitate to ask.
Add OOP to your scripts via the Class Library. Check out my scripts. |
|
| Back to top |
|
 |
Sean
Joined: 12 Feb 2007 Posts: 2141
|
Posted: Sat Apr 25, 2009 11:49 pm Post subject: |
|
|
| animeaime wrote: | See what I mean, yal are in a different league... that RegEx makes no sense to me, but it works - somehow  | You just don't know yet some notations/conventions in RegEx. I just translated into RegEx what I would actually do without considering any performance.
| Code: | "C:\FolderA\FolderB\FolderC\"
RegExMatch(Folder,"^.*\\(?=[^\\]+\\?$)",Root)
|
|
|
| Back to top |
|
 |
animeaime
Joined: 04 Nov 2008 Posts: 1046
|
Posted: Sat Apr 25, 2009 11:51 pm Post subject: |
|
|
OK, after reviewing it, I think I get it. I modified it slightly because I don't want to backtrace to a root folder - thanks.
| Code: | ; Folder := "C:\FolderA\FolderB\FolderC"
Folder := "\FolderA\FolderB\FolderC\"
Folder := "C:\FolderA\"
Folder := "\FolderA"
if RegExMatch(Folder,"i)(?:[A-Z]:)?\\.*\\(?=[^\\]+\\?$)",Root)
MsgBox % Root
|
_________________ As always, if you have any further questions, don't hesitate to ask.
Add OOP to your scripts via the Class Library. Check out my scripts. |
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 2428
|
Posted: Sun Apr 26, 2009 12:10 am Post subject: |
|
|
| animeaime wrote: | See what I mean, yal are in a different league... that RegEx makes no sense to me, but it works - somehow  |
Not to be a shameless self-promoter (but I will), you can always check out my beginner's RegEx tutorial for AHK, it'll get you into the basics of RegEx with some slightly more advanced techniques along the way. But I'll give you a freebie on this one since Sean's answer fascinates me in its simplicity that completely eluded me:
The circumflex (^) is a start anchor, meaning that any matching pattern must start with whatever comes after it. In this case, the start anchor says that any matching pattern must start with zero or more of (almost) any characters (.*) followed by a literal backslash (\\).
Anytime you see (?=...) in use that is a positive look-ahead assertion, meaning anything contained within the parenthesis must exist within the pattern in order to be a match. In this case the pattern that must exist is this:
Where \\ is a literal dash and the circumflex is usually a RegEx start anchor, putting them together in the character class brackets like this ([^\\]) means any character that is NOT a literal dash, and the plus directly after it means that it will match one or more characters that is not a literal dash.
That pattern is followed by a literal dash, so now we're looking for one or more characters that is not a literal dash, followed by a literal dash.
The question mark means that the preceding character is optional, so now we're looking for one or more characters that is not a literal dash optionally followed by a literal dash.
The dollar sign ($) is an end anchor, meaning that any matching pattern must end with whatever precedes it. So now our positive look-ahead assertion says that we're looking for a pattern that will end with one or more characters that is not a literal dash, optionally followed by a literal dash.
The significance of the positive look-ahead assertion as opposed to a method like I used has to do with character consumption, which we won't go into here.
So the whole thing:
| Code: | | ^.*\\(?=[^\\]+\\?$) |
We're looking for a pattern that starts with zero or more of (almost) any characters followed by a literal dash and ends with one or more characters that are not a literal dash, optionally followed by a literal dash.
Hopefully that makes sense. _________________ Try Quick Search for Autohotkey or see the tutorial for newbies. |
|
| Back to top |
|
 |
animeaime
Joined: 04 Nov 2008 Posts: 1046
|
Posted: Sun Apr 26, 2009 12:14 am Post subject: |
|
|
Thanks for the quick tutorial. _________________ As always, if you have any further questions, don't hesitate to ask.
Add OOP to your scripts via the Class Library. Check out my scripts. |
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 2428
|
Posted: Sun Apr 26, 2009 1:03 am Post subject: |
|
|
| animeaime wrote: | | Thanks for the quick tutorial. |
Shoot, that's the easy stuff. I've had your OOP files for weeks now and still haven't wrapped my head around the implications of what I'm looking at.  _________________ Try Quick Search for Autohotkey or see the tutorial for newbies. |
|
| Back to top |
|
 |
animeaime
Joined: 04 Nov 2008 Posts: 1046
|
Posted: Sun Apr 26, 2009 1:10 am Post subject: |
|
|
Feel free to ask questions on the Class library (OOP) - Help Thread. I'm trying to update the docs, but I keep getting distracted - I hate writing docs. Writing code is easy, docs, not so much. _________________ As always, if you have any further questions, don't hesitate to ask.
Add OOP to your scripts via the Class Library. Check out my scripts. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 7159
|
Posted: Sun Apr 26, 2009 6:50 am Post subject: |
|
|
| animeaime wrote: | | what's the difference between using a DllCall and simply removing the backslash? |
I said "I would do it like" - only because it was shorter.
SubStr+StringTrimRight should be faster.
PhiLho once suggested me a simple RegEx: RegExReplace( Folder, "\\$") |
|
| Back to top |
|
 |
|