Sign in to follow this  
Followers 0
Gibbo

_PathSplitRegEx

7 posts in this topic

#1 ·  Posted (edited)

Recently I had the need to split paths like _PathSplit outside of AutoIt.

Regular expressions seemed to be the way to go and it was fun to learn a bit more about their power.

I backported it to AutoIt and now use it instead of _PathSplit.

This should match any combination of:

servershare[$] | Drive
path
filename
.extention

Some caveats:

It only returns an array (returning strings via byref aswell seemed a tad overkill / messy)

The Drive letter retains its trailing "" NOT the path (this seemed more sane as the path should be relative not anchored to the root of the drive) - Found that this was not so good for things like "C:" as the path shoud be set to "" not nothing

; #FUNCTION# ====================================================================================================================
; Name...........: _PathSplitRegEx
; Description ...: Splits a path into the drive, directory, file name and file extension parts. An empty string is set if a part is missing.
; Syntax.........: _PathSplitRegEx($sPath)
; Parameters ....: $sPath - The path to be split (Can contain a UNC server or drive letter)
; Return values .: Success - Returns an array with 5 elements where 0 = original path, 1 = drive, 2 = directory, 3 = filename, 4 = extension
; Author ........: Gibbo
; Modified.......:
; Remarks .......: This function does not take a command line string. It works on paths, not paths with arguments.
;                This differs from _PathSplit in that the drive letter or servershare retains the "" not the path
;                RegEx Built using examples from "Regular Expressions Cookbook (O’Reilly Media, 2009)"
; Related .......: _PathSplit, _PathFull, _PathMake
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _PathSplitRegEx($sPath)
if $sPath="" then Return SetError(1,0,"")
Local Const $rPathSplit = '^(?i)([a-z]:|[a-z0-9_.$]+[a-z0-9_.]+$?)?((?:[^/:*?"<>|rn]+)*)?([^/:*?"<>|rn.]*)((?:.[^./:*?"<>|rn]+)?)$'
Local $aResult = StringRegExp($sPath, $rPathSplit, 2)
Switch @error
Case 0
Return $aResult
Case 1
Return SetError(2, 0, "")
Case 2
;This should never happen!
EndSwitch
EndFunc ;==>_PathSplitRegEx

Anyhow, I hope someone else finds this useful :D

Edit: @ScriptFullPath replaced with the parameter $sPath - Thanks czardas. Should have reread before posting.

Edit2: Changed regex to move the trailing "" from the "root" to the begining of the path.

Edit3: Edit destroyed the regex (why??) added below as well in plain code tags just in case it happens again:

Local Const $rPathSplit = '^(?i)([a-z]:|[a-z0-9_.$]+[a-z0-9_.]+$?)?((?:[^/:*?"<>|rn]+)*)?([^/:*?"<>|rn.]*)((?:.[^./:*?"<>|rn]+)?)$'
Edited by Gibbo
1 person likes this

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

There appears to be an editing msitake in this function. @ScriptFullPath needs to be replaced with the parameter $sPath , it's the sort of thing I might overlook myself during testing. Not examined it in detail, but it seems to be working.

Edited by czardas

Share this post


Link to post
Share on other sites
1 person likes this

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

@czardas - Thanks for picking that up :D

@MrCreatoR

I took a look and tested:

#include <File.au3>
#include <Array.au3>
$aSplit = _PathSplitByRegExp("serversharepathfile.ext")
_ArrayDisplay($aSplit)

$aSplit = _PathSplitRegEx("serversharepathfile.ext")
_ArrayDisplay($aSplit)

Local $szDrive, $szDir, $szFName, $szExt
Local $TestPath = _PathSplit("serversharepathfile.ext", $szDrive, $szDir, $szFName, $szExt)
_ArrayDisplay($TestPath, "Demo _PathSplit()")

Yours seems to fail on UNC paths

[0]|serversharepathfile.ext
[1]|
[2]|serversharepath
[3]|serversharepathfile
[4]|serversharepathfile.ext
[5]|serversharepath
[6]|file.ext
[7]|file
[8]|ext

Also returning all those combinations seems a bit of overkill considering any of those could be reconstructed from just "root,folder,file,ext" :huh:

[0]|serversharepathfile.ext
[1]|servershare
[2]|path
[3]|file
[4]|.ext

Mine groups the share with the server name as this is the "root" of any folder structure in a share.

[0]|serversharepathfile.ext
[1]|server
[2]|sharepath
[3]|file
[4]|.ext

_PathSplit does a good job but groups the share name with the path and I didn't much care for all the "ByRef" strings and such when $aSplit[0] -> $aSplit[4] already contained the values.

All up though it was more of a fun exercise to make/port. I found it useful and just thought others would too.

:)

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

I took a look and tested:

Your function does not return an array, and what shouldn't never happen, happened:

#include <Array.au3>

$aSplit = _PathSplitRegEx("serversharepathfile.ext")
_ArrayDisplay($aSplit)

Func _PathSplitRegEx($sPath)
    if $sPath="" then Return SetError(1,0,"")
    Local Const $rPathSplit = '^(?i)([a-z]:|[a-z0-9_.$]+[a-z0-9_.]+$?)?((?:[^/:*?"<>|rn]+)*)?([^/:*?"<>|rn.]*)((?:.[^./:*?"<>|rn]+)?)$'
    Local $aResult = StringRegExp($sPath, $rPathSplit, 2)
    Switch @error
        Case 0
            Return $aResult
        Case 1
            Return SetError(2, 0, "")
        Case 2
            MsgBox(64, 'Title', 'This should never happen? Well, it''s happened :)')
    EndSwitch
EndFunc

:)

Edited by MrCreatoR

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

AutoIt is simple, subtle, elegant. © AutoIt Team

Share this post


Link to post
Share on other sites

The reason it didn't work for you is that the forums seem to have destroyed the regex

I had:

Local Const $rPathSplit = '^(?i)([a-z]:|\\\\[a-z0-9_.$]+\\[a-z0-9_.]+\$?)?(\\(?:[^\\/:*?"<>|\r\n]+\\)*)?([^\\/:*?"<>|\r\n.]*)((?:\.[^.\\/:*?"<>|\r\n]+)?)$'

You used:

Local Const $rPathSplit = '^(?i)([a-z]:|[a-z0-9_.$]+[a-z0-9_.]+$?)?((?:[^/:*?"<>|rn]+)*)?([^/:*?"<>|rn.]*)((?:.[^./:*?"<>|rn]+)?)$'

Share this post


Link to post
Share on other sites

So often I find that I only need part of the filename, ie, just the ext, or just the Drive, so I created these fairly simple functions.

Func File_GetDrive($sFilespec)
    Return StringRegExpReplace($sFilespec, '^([A-Za-z]:)?(?:.*)$', '1')
EndFunc
Func File_GetPath($sFilespec)
    If Not StringRegExp($sFilespec, "", 0) Then Return ''
    Return StringRegExpReplace($sFilespec, '^([A-Za-z]:|[A-Za-z]:||)(.*)?(?:[^]+)?$', '2')
EndFunc
Func File_GetName($sFilespec)
    If StringRegExp($sFilespec, "$", 0) Then Return ''
    Return StringRegExpReplace($sFilespec, '^(?:[A-Za-z]:|)(?:.*|)(.*?)(?:.[^.]*|)$', '1')
EndFunc
Func File_GetExt($sFilespec)
    If StringRegExp($sFilespec, "$", 0) Or Not StringRegExp($sFilespec, ".", 0) Then Return ''
    Return StringRegExpReplace($sFilespec, '^(?:[A-Za-z]:|)(?:.*|)(?:.*?)(.[^.]*)?$', '1')
EndFunc
Func File_GetDriveAndPath($sFilespec)
    Return StringRegExpReplace($sFilespec, '^([A-Za-z]:|)(?:|)(.*|)(?:[^]*)', '12')
EndFunc
Func File_GetNameAndExt($sFilespec)
    Return StringRegExpReplace($sFilespec, '^(?:[A-Za-z]:|.*|)([^.].*)?(.[^.]*)?$', '1')
EndFunc
Func File_GetParent($sFilespec)
    Local $vParts = StringRegExp($sFilespec,"(.*)([^]+?)$",3)
    If Not @error Then Return $vParts[0]
EndFunc
Func Win_FilenameSafe($sFilespec)
    Return StringRegExpReplace($sFilespec,"x00|x01|x02|x03|x04|x05|x06|x07|x08|x09|x0A|x0B|x0C|x0D|x0E|x0F|x10|x11|x12|x13|x14|x15|x16|x17|x18|x19|x1Ax1B|x1C|x1D|x1E|x1F|x22|x2A|x2F|x3A|x3C|x3E|x3F|x5C|x7C|x81|x8D|x8F|x90|x9D|xAD","")
EndFunc

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Similar Content

    • TheAutomator
      By TheAutomator
      Can anyone tell me why this isn't working?..
      #include <array.au3> $regexp = StringRegExp("test 'a b c'", "'([^']|'')*'|\S+", 3) _ArrayDisplay($regexp) trying to split this "test 'a b c'  'some other '' test'' ...'" into:
      0: test
      1: 'a b c'
      2: ...
      but it gives me:
      0: test
      1: c
    • anthonyjr2
      By anthonyjr2
      Hi guys,
      I am pretty bad with regex, and am having some trouble trying to come up with an expression for a certain type of string. Basically I want to be able to tell if a string is of the format:
      AA#####A
      Where the A's are any letter from A-Z and the #'s are any digit from 0-9.
      I've been playing around with a regex tester online for a while but I can't really seem to grasp the concept very well. Could anyone give me any tips?
      This isn't exactly an AutoIt specific question which is why I didn't post it in General Help & Support.
    • tezhihi
      By tezhihi
      I have a file (see attached file) with a string all line and this problem on here is I want to separate all $00:, $03:, $10:, $20:, $25:, $30:, $40:, $45:, $110:, $115:, $120: and $T. It's mean that each $ with value start a new line ( a new paragraph). I tried with Regular Expression in notepad++ ex:
      Find ($00:, $01:, $03: and so on) with regex (\$)([0-9]+): and replace is \r\n\1\2 (I think \r\n is @CRLF (not sure :() ) Find $T with regex (\$T)(.*?)(\$T) and replace is \1\2\r\n\3 When I try these regex to replace in notepad on StringRegexReplace the results is incorrect . I have read some example simple about regex. Please advise me how to do that with some example on autoit . The result will be in attached photo. Thanks 
      ahihi.txt

    • MyEarth
      By MyEarth
      Hello, i need to validate a string can be different things. I just need a True - False return value, no groups or things like that. It will be always one line at time to be processed by StringRegEx
      Valid:
      13:52|String
      02:52 XX|String
      13:52~SUN, MON, TUE, WED, THU, FRI, SAT|String
      02:52 XX~SUN, MON, FRI|String
      22/04/2017 13:52|String
      22/04/2017 02:52 YY|String
      Not Valid
      22/04/2017 13:52~Dom|String
      I need to validate until and inclusively the | after that i don't care
      The XX and YY value are two $sVariable from my script
      SUN, MON, TUE, WED, THU, FRI, SAT are fixed value, the can be mixed but always in the same order like
      SUN
      SUN, TUE, WED
      SUN, SAT 
      The time can be 12 or 24 hours, the date is always in the same format DD/MM/YYYY. If there is a date can't be a day after that ( see not valid )
      Well i think is all
      Sorry if i don't provide a working code, regex is too way complex.
      Thanks
    • DavidLago
      By DavidLago
      Hello. 
      I need to list only the folders that has a name composed of numbers only.
      How do I do this?
      OBS: I just posted the relevant content to the folder.
      Global $path = "G:\jobs\" For $i = 1 To $aArray[0] Local $search = FileFindFirstFile($path & $aArray[$i] & "\[0-9]" ) I tried with "\*.*" and it returns me the expected result, but the other doesn't.