I need a regex that can parse the following example file paths and return the path, base, ext and num as shown in the following table.
This regex almost works, but unfortunately it falls short when the filename contains multiple periods.
^(.*\\)?(.*?)\.(.*?)\.?([0-9]+)?$
input path base ext num
--------------------------- ------------ ---------- ------ ------
c:\temp\00883005.prt.1 c:\temp\ 00883005 prt 1
00883005.prt 00883005 prt
00883005.prt.1 00883005 prt 1
\\data\test\00883005.prt \\data\test\ 00883005 prt
\\data\test\00883005.prt.1 \\data\test\ 00883005 prt 1
00883005.test.prt 00883005.test prt
00883005.test.prt.1 00883005.test prt 1
c:\temp\00883005.test.prt.1 c:\temp\ 00883005.test prt 1
00883005.mp3 00883005 mp3
Here is the working AutoHotkey code (thanks to the folks below):
RegExMatch( filename, "^(.*\\)?(.*?)\.([^\.]*?)\.?(\b[0-9]+)?$", data)
outputdebug % "dir = " data1
outputdebug % "base = " data2
outputdebug % "ext = " data3
outputdebug % "num = " data4
Try this version:
The difference is the second-to-last group: Changed
([^\.]*?)to(.*?)such that dot is not included in the match.Tested on the great http://www.regexplanet.com against the provided data.