I'll admit: I'm not a regex expert At All, so be gentle with me...
What I'm looking for, applies to the File Rename command in D0 9.
I have a set of files named like this:
Some_name (01 of 03)(some junk) (some more junk).txt
Another_name (02 of 06) (get rid of this) (get rid of this too).txt
I would like to have them renamed to:
Some_name (01 of 03).txt
Another_name (02 of 06).txt
So, everything between brackets can be removed, Except when it contains the string " of " (i.e. the word 'of', preceded and followed by a space).
Thanks for your reply, MrC. I tried it, but nothing happens, i.e. nothing is replaced at all
I copied your new name string, so there I wouldn't have made a mistake. Any ideas, anyone? I utterly stuck here. Regex is like medieval Chinese to me.
Make sure you set the type to Regular Expressions before pasting in the Old Name string, else the pasted string may be modified based on which characters are valid in the type it was set to.
Hi Leo. A correction: it does work, but it treats the presence of "02 of 06" etc. as a condition, not as an exception, i.e. everything between brackets is removed except "02 of 06" Only When "02 of 06" is present in the file name.
My goal is to preserve "02 of 06", but also to remove everything between brackets when "02 of 06" is not present.
Example:
Some_name (01 of 03)(some junk) (some more junk).txt
Another_name (02 of 06) (get rid of this) (get rid of this too).txt
Some_other_name (some other rubbish).txt
Should become:
Some_name (01 of 03).txt
Another_name (02 of 06).txt
Some_other_name.txt
Here's a perlscript which solves the problem more elegantly.
[code]@script perlscript
sub Rename_GetNewName2
{
my ($strFileName, $strFilePath, $fIsFolder, $strOldName, $strNewName) = @;
$strFileName =~ s/ ?((?!\d+ of \d+)[^)]*)//g;
return $strFileName;
}[/code]
The idea is straightforward - strip each paren grouping, unless it contains your Num of Num pattern. This is conceptually simpler, and more robust than having to attempt recursive capture/replace groups with the limited REs in Dopus 9.
You'll need activeperl installed. I'll explain how it works if you're interested.
If you don't have/want ActivePerl installed just for this, here's a VBScript version that you can use without anything extra. (VBScript is built into Windows.) Paste it into the same place shown in MrC's screenshot above.
[code]@script vbscript
option explicit
Function Rename_GetNewName ( strFileName, strFullPath, fIsFolder, strOldName, ByRef strNewName )
' Create a RegExp object. See http://msdn2.microsoft.com/en-us/library/ms974570.aspx
Dim re
Set re = new RegExp
re.IgnoreCase = True
re.Global = False
re.Pattern = "\([0-9]+ of [0-9]+\)"
If (re.Test(strNewName)) Then
re.Pattern = "(\([0-9]+ of [0-9]+\))( *\([^)]*\))+"
strNewName = re.Replace(strNewName, "$1")
Else
re.Pattern = "( *\([^)]*\))+"
strNewName = re.Replace(strNewName, "")
End If
End Function[/code]
Note: This won't handle extra text between each set of brackets, like (stuff in brackets) some other stuff (more stuff in brackets) since I don't think that was ever specified as a requirement. If you do need that it would be easy enough to add. (MrC's version does handle the extra text. So the two scripts don't do exactly the same thing.)