@rur54
Indeed, depending on your real files, it might be easier to "dismantle" file names step by step like you did…
@rur54 &
@all
But since I’d started, I thought I might as well use this opportunity to make a tiny
Regex Tutorial, for anyone interested!
(With a cleaner regex that is more readable and, hopefully, tweakable!)
BTW, the original regex works on real files too (with the names you provided) if used in the script below, and with gpc($item, "file") instead of "base".
⮚ So here’s a practical case with a cleaner regex that processes each file one by one:
• Original files:
doc1_by_author.doc
doc2_by_author-temp.doc
doc3_by_author [tag1 tag2].doc
doc4_by_author-temp [tag1 tag2].doc
• Renamed files:
author_doc1.doc
author_doc2.doc
author_doc3 [tag1 tag2].doc
author_doc4 [tag1 tag2].doc
Code: Select all
$List = listpane(, , ,<crlf>);
foreach($item, $List, <crlf>) {
$newName = gpc($item, "base");
$newName = regexreplace($newName, "^(.+)_by_([^\[-]+[^\s\[-])[^\[]*( \[[^\]]+\])*$", "$2_$1$3");
renameitem("$newName", "$item", , "-01");
}
⮚ So we have:
— the “title”: everything from the start (
^), until (but not including) _by_
^(.+)_by_
— the “author”: everything that follows but doesn’t include
–(temp) or
[(tags), and ends without a space (
\s) to keep it for potential tags!
([^\[-]+[^\s\[-])
— some potential “-temp” suffix to discard: anything that starts with
– but doesn’t include potential
[(tags)
[^\[]*
— some potential “tag”: anything that starts with a
space, is then included in
[brackets
], and is at the end of the file (
$)
( \[[^\]]+\])*$
Hope this might help someone!
Have a nice day,
Flux