Jump to content

StringRegExp with optionals


Recommended Posts

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

  • Moderators

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

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

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

PrimeMover,

a lot better than trying to actually use optionals

Actually there is an optional in there: :oops:

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! :rip:

Glad I could help. :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Let me a chance. I take specification 4) as meaning something like

Exterior Store (pink) Pan (03).MP4

Then 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).MP4

Note 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 here
RegExp tutorial: enough to get started
PCRE 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

  • Moderators

jchd,

I just knew a real guru would do it in one! :D

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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 here
RegExp tutorial: enough to get started
PCRE 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

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