Jump to content

StringRegExp: extract Date from Pathname


Recommended Posts

Hi everybody,
 
I'd like to extract the Date (Folder Name) from a path that is saved as Local Constant:

Local $Date=0
Local Const $metaPath= @ScriptDir & "\warra\2015\150128\procEagle\rad\EA_warra_test.meta"
;Local Const $Date= "20" & StringRegExp($metaPath, '([:digit:]{6})', 1)
$Date= StringRegExp($metaPath, '(?:\)(\d{6})(?:\)', 0)

I want to get the 150128, to send it  to a text file later

Send("Time:{ENTER}Date{TAB}" & $Date & "^s")

But no matter what I try, I get no matches (output '0' or nothing at all). Other operations are able to work with $metaPath.

Where is my error?
 
Thanks for replies.

Link to comment
Share on other sites

First: setting your second argument in the StringRegExp to 0 results in the call just returning 0 for no match, and 1 for match. You will not get the actual matching substring.

Second: your regular expression is wrong anyway :P

AFAICT you are just looking for the first occurence of a string consisting of 6 digits, enclosed by backslashes. I see what you tried to do with the non-capturing groups, but they are not necessary. What you failed to do is escaping the backslashes. If you want a literal backslash in a regular expression, you have to escape it by using two backslashes. Try this:

$metaPath= @ScriptDir & "\warra\2015\150128\procEagle\rad\EA_warra_test.meta"

$Date = StringRegExp($metaPath, '\\(\d{6})\\', 3)
; note the 3. It instructs the function to return an array of global matches. (See helpfile for more information.)

if @error Then
    msgbox(16, "error", "date not found in string!")
    Exit
Else
    msgbox(64, "super ultra turbo mega result!", $Date[0])
    ; The first match of the captured pattern is stored in array element 0.
    ; As you only want the first match, we don't care about subsequent matches,
    ;    so we just hardcode the use of the first element (which is element 0)
    ;    and disregard any other array elements that may or may not exist.
    ; If we do care about subsequent matches, we can find them in subsequent array elements.
EndIf
Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Well, to be complete, as I mentioned, you also formulated your regex incorrectly and called StringRegExp in a way that never returns an array. (0 as second argument.) I changed that to 3, which is the flag to "Return array of global matches." See the table with "Flags - Values" early in the StringRegExp helpfile.

Edited by SadBunny

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...