PrimeMover Posted December 6, 2011 Share Posted December 6, 2011 I'm trying to parse a filename that has an incremental number in it, for organizing media files. I've been trying to use StringRegExp to perform the procedure, but I've been having problems with designating optionals. The tutorials say that optionals are created by putting a "?" after a group, but every time I do it, the optional match completely ceases to function. Let me start over with what I'm doing. Here's a possible string: 'Exterior Store Pan (03).MP4' I want to separate the string into three different componants: the name, the incremend number and the extension. Here are the conditions 1. The incremental number may or may not exist 2. The number match needs to remove the parenthesis 3. There may or may not be a space before the number, if so, it needs to be removed from the name match 4. other parenthetical text may appear before the number, in the name, and must be evaluated as part of the name The problem is, I'm having real trouble with these optional statements. I'm using (.*) to capture everything up to the parenthesis, but if the parenthesis doesn't exist, the function errors. Also, I don't know how to capture only a parenthesis with numbers in it. I understand all the basics of creating matches, but with this conditionals, everything becomes quite baffling. I want to make this program very dummy proof, and able to evaluate any number of strange formats, within reason. Any help would be much appreciated. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 6, 2011 Moderators Share Posted December 6, 2011 PrimeMover, I would forget trying to get it all in one SRE - although no doubt one of the gurus will be along shortly to show you how. Why not do it in several steps like this: Global $aName[4] = ["Exterior Store Pan (03).MP4", "Exterior (Store) Pan.MP4", "Exterior Store Pan ( 07).MP4", "Exterior (Store) Pan ( 02).mp4"] For $i = 0 To UBound($aName) - 1 ; Is there an incremental number in parentheses with a possible space before it If StringRegExp($aName[$i], "(s?d+)") Then ; The name runs to the final ( $sName = StringRegExpReplace($aName[$i], "(.*)(.*", "$1") ; The number is in the final set of () - and we remove any white space aftrewards $sNumber = StringStripWS(StringRegExpReplace($aName[$i], ".*((.+)).*", "$1"), 1) Else ; The name runs to the .ext $sName = StringRegExpReplace($aName[$i], "(.*)..*", "$1") ; There is no incremental number $sNumber = "" EndIf ; And teh extension is always after the final . $sExt = StringRegExpReplace($aName[$i], ".*.(.+)", "$1") ; And here are the results MsgBox(0, "Result", $sName & @CRLF & $sNumber & @CRLF & $sExt) Next Good enough? Let me know if you find a format that does not work. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
PrimeMover Posted December 6, 2011 Author Share Posted December 6, 2011 Thanks! That did it! Yeah, a lot better than trying to actually use optionals. I was so stuck in a rut, I didn't think about doing it the more conventional way. Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 6, 2011 Moderators Share Posted December 6, 2011 PrimeMover, a lot better than trying to actually use optionalsActually there is an optional in there: StringRegExp($aName[$i], "(s?d+)") ; s? = a space which may or may not be there I often fall foul of the "all in one SRE" syndrome. You want the optimal solution and get too complicated - which is always fatal! Glad I could help. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
jchd Posted December 6, 2011 Share Posted December 6, 2011 Let me a chance. I take specification 4) as meaning something like Exterior Store (pink) Pan (03).MP4Then from what I understand you can use the following :$res= StringRegExp($aName[$i], "^(.*?)(?:(?:s*((d+)))?(..*))$", 1)You should get your 3 components as $res[0] to $res[2]This worked on the following inputs:Exterior Store Pan.MP4 Exterior Store Pan (03).MP4 Exterior Store (pink) Pan.MP4 Exterior Store (pink) Pan (03).MP4 Exterior (strong) Store type (03) color (pink) Pan (03).MP4Note that it will fail on "Exterior (strong) Store model abc (05).mp4 type (03) color (pink) Pan (03).MP4"Given that the last sentence "I want to make this program very dummy proof, and able to evaluate any number of strange formats, within reason." opens a wide room for madness, I won't consider that a problem. If you need that anyway, it can still be dealt with. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted December 6, 2011 Moderators Share Posted December 6, 2011 jchd,I just knew a real guru would do it in one! M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
jchd Posted December 6, 2011 Share Posted December 6, 2011 This one is fairly simple compared to what can be seen elsewhere. Long ago I could have done it as a one-liner really supercryptic APL but PLEASE don't ask now! This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now