Jump to content
Sign in to follow this  
Gibbo

_PathSplitRegEx

Recommended Posts

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

Share this post


Link to post
Share on other sites

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

 

Spoiler

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...

Spoiler

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

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

 

Spoiler

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...

Spoiler

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By genius257
      Inspired by PHP's preg_split.
      Split string by a regular expression.
      Also supports the same flags as the PHP equivalent.
      v1.0.1
       
      Example:
      #include "StringRegExpSplit.au3" StringRegExpSplit('splitCamelCaseWords', '(?<=\w)(?=[A-Z])') ; ['split', 'Camel', 'Case', 'Words']  
    • By BlueBandana
      Is there a way to output the regex matches into a file?
      I have a script to compare two files and check for regex matches.
      I want to output the matching regex of 'testexample.txt' to another file.
      #include <MsgBoxConstants.au3> #include <Array.au3> $Read = FileReadToArray("C:\Users\admin\Documents\testexample.txt") $Dictionary = FileReadToArray("C:\Users\admin\Documents\example.txt") For $p = 0 To UBound($Dictionary) - 1 Step 1 $pattern = $Dictionary[$p] For $i = 0 To UBound($Read) - 1 Step 1 $regex = $Read[$i] If StringRegExp($regex, $pattern, 0) Then MsgBox(0, "ResultsPass", "The string is in the file, highlighted strings: " ) Else MsgBox(0, "ResultsFail", "The string isn't in the file.") EndIf Next Next  
    • By guner7
      Hello,
      I need some help to parse the Green highlighted value with from below text:
      RESISTOR  THICK FILM 4.64K ±1% 1/4W ±100PPM/°C 1206 SMT
      RESISTOR  THICK FILM 3.83K ±1% 1/4W ±100PPM/°C 1206 SMT
      RESISTOR CARBON FILM 22K ±10% 1/2W AXIAL THT
      RESISTOR  WIREWOUND  22 ±5% 3W ±30PPM/°C AXIAL THT
      RESISTOR  METAL OXIDE 4.7K ±5% 2 W ±300PPM/°C AXIAL THT
      RESISTOR  THICK FILM 0   1/8W  0805 SMT
      I am using positive look behind.:
      (?<=FILM|WOUND|OXIDE).+ Can only pull this off:
      4.64K ±1% 1/4W ±100PPM/°C 1206 SMT 3.83K ±1% 1/4W ±100PPM/°C 1206 SMT 22K ±10% 1/2W AXIAL THT  22 ±5% 3W ±30PPM/°C AXIAL THT 4.7K ±5% 2 W ±300PPM/°C AXIAL THT 0   1/8W  0805 SMT I'm trying the \b word boundary to no avail at this point. Appreciate if anyone would guide me on this?
    • By junichironakashima
      Im creating a code that will work in this sequence:
      1. Copy the text (question) in one atea of the screen
      2. Catch the 2 strings (number)
      3. Multiply the 2 strings ( $1*$2)
      4. Click the next area to put the answer
      5. Paste the answer
       
      This is my code
       
      MouseClick($MOUSE_CLICK_LEFT, 479, 802, 3, 1) ;Clicking all of the text
      Send("^c") 
      $x = StringRegExpReplace(ClipGet(), 'What is (\d*) x (\d*) \?$', "$1*$2")
      MouseClick($MOUSE_CLICK_LEFT, 480, 844, 1, 1)
      ClipPut($x)
      Send("^v")
       
      However the output is this
      $1*$2
       
      How can I make it solve itself? Because I tried this code:
      MouseClick($MOUSE_CLICK_LEFT, 479, 802, 3, 1) ;Clicking all of the text
      Send("^c")
      MouseClick($MOUSE_CLICK_LEFT, 480, 844, 1, 1) $x = Execute(StringRegExpReplace(ClipGet(), 'What is (\d*) x (\d*) \?$', "$1*$2"))
      ClipPut($x)
      Send("^v")
      Output is just blank text

    • By gruntydatsun
      I have an XML file and every time there are three lines in a row with only <null/> in them, i want to insert a fourth line with <null/>.   Each line starts with 3 white spaces, followed by <null/> and ends with a white space followed by CR LF.   The presence of the three lines as described is unique to the points where I want to insert a line in this document.
       I'm trying to figure out how to apply the repeating part of a regex  {1,4} but apply it to this whole segment. 
      So far I have the below which picks up an individual line ok:
      ^\s{3}<null/>\s\r\n I tried wrapping it all in braces () then adding {3} but I'm obviously getting something wrong. 
      Attached is a section from the xml file with a block of nulls that should be matched if anyone would like to have a look.
      Help_From_Forum.xml
×
×
  • Create New...