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 

inStr question
Goto page Previous  1, 2
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
sinkfaze



Joined: 18 Mar 2008
Posts: 2428

PostPosted: Sat Apr 25, 2009 11:34 pm    Post subject: Reply with quote

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
View user's profile Send private message
sinkfaze



Joined: 18 Mar 2008
Posts: 2428

PostPosted: Sat Apr 25, 2009 11:35 pm    Post subject: Reply with quote

@ Sean

Code:
RegExMatch(Folder,"^.*\\(?=[^\\]+\\?$)",Root)


D'OH! How did I miss an answer that easy?
_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message
animeaime



Joined: 04 Nov 2008
Posts: 1046

PostPosted: Sat Apr 25, 2009 11:36 pm    Post subject: Reply with quote

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 Shocked
_________________
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
View user's profile Send private message Send e-mail
Sean



Joined: 12 Feb 2007
Posts: 2141

PostPosted: Sat Apr 25, 2009 11:49 pm    Post subject: Reply with quote

animeaime wrote:
See what I mean, yal are in a different league... that RegEx makes no sense to me, but it works - somehow Shocked
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
View user's profile Send private message
animeaime



Joined: 04 Nov 2008
Posts: 1046

PostPosted: Sat Apr 25, 2009 11:51 pm    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
sinkfaze



Joined: 18 Mar 2008
Posts: 2428

PostPosted: Sun Apr 26, 2009 12:10 am    Post subject: Reply with quote

animeaime wrote:
See what I mean, yal are in a different league... that RegEx makes no sense to me, but it works - somehow Shocked


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. Wink But I'll give you a freebie on this one since Sean's answer fascinates me in its simplicity that completely eluded me:

Code:
^.*\\


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 (\\).

Code:
(?=[^\\]+\\?$)


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:

Code:
[^\\]+\\?$


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
View user's profile Send private message
animeaime



Joined: 04 Nov 2008
Posts: 1046

PostPosted: Sun Apr 26, 2009 12:14 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
sinkfaze



Joined: 18 Mar 2008
Posts: 2428

PostPosted: Sun Apr 26, 2009 1:03 am    Post subject: Reply with quote

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. Smile
_________________
Try Quick Search for Autohotkey or see the tutorial for newbies.
Back to top
View user's profile Send private message
animeaime



Joined: 04 Nov 2008
Posts: 1046

PostPosted: Sun Apr 26, 2009 1:10 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail
SKAN



Joined: 26 Dec 2005
Posts: 7159

PostPosted: Sun Apr 26, 2009 6:50 am    Post subject: Reply with quote

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. Smile
SubStr+StringTrimRight should be faster.

PhiLho once suggested me a simple RegEx: RegExReplace( Folder, "\\$")
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
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