Apply Replace Method to part of a name

Advanced Renamer forum
#1 : 14/06-21 06:56
matias
matias
Posts: 3
Hi! I'm super new to this app and regex in general, so I would appreciate some help, I have some thousands .pdf files in this format:

*some title*, *subtitle* - *author*, *author* (in case of 2 authors, they could be more)

I want to replace the second and all subsequent appearances of the comma with an &, leaving any commas before de "-" intact

Can it be done?


14/06-21 06:56
#2 : 14/06-21 16:22
David Lee
David Lee
Posts: 1125
Replace: ([^-]*[^,]*),
with: \1 &
Occurrence: All
Use regular expressions

[^-]* matches a string of any number of any characters except "-"
[^,]* matches a string of any number of any characters except ","

So the Regex ([^-]*[^,]*) will match everything up to the first hyphen plus all following characters up to, but not including, the next comma. Enclosing the regex in parentheses saves the complete matched string in a variable: \1.

Finally, "," adds the next comma to the match, but doesn't save it.

Specifying "Occurrence = All" means that the regex will be repeated on the remaining characters until the end of the filename is reached.


14/06-21 16:22 - edited 14/06-21 16:23
#3 : 14/06-21 18:25
matias
matias
Posts: 3
Reply to #2:

Hi, first of all thank you for the reply and your explanation, your solution answers the problem I posted but the results made me realize there are many possible scenarios that I didn't think about

First, I realized some books have commas as part of their title (for example "Explorations in Timbre, Texture, And Space"), other titles don't have a subtitle, and other files don't even have an author specified (I have to fix that by other means so we can ignore it), and those facts kind of breaks the logic of the regex in some cases, for example:

"Explorations in Timbre, Texture, and Space" transforms to "Explorations in Timbre, Texture & And Space"

"1,500 Stretches, The Complete Guide to Flexibility And Movement - Hollis Liebman" transforms to "1,500 Stretches & The Complete Guide to Flexibility And Movement - Hollis Liebman"

"1910, The Emancipation Of Dissonance - Thomas Harrison" transforms to "1910 & The Emancipation of Dissonance - Thomas Harrison"

I realize my mistake was to use such a common symbol as a comma to separate fields, but I guess my question, reformulated, would be:

Is there a way to apply a reply method ONLY to the section past the "-", leaving everything before that intact?



14/06-21 18:25
#4 : 14/06-21 19:14
matias
matias
Posts: 3
Reply to #3:

I meant "replace method"* instead of "reply method" sorry


14/06-21 19:14
#5 : 15/06-21 00:57
David Lee
David Lee
Posts: 1125
Reply to #4:
An improvement would be to use a "negative lookahead"...

Replace: ",(?!(.*-))"
with: " &"
(Don't include the quotation marks).

This will only replace a comma if it is NOT followed by a string containing "-".

However this will replace ALL commas if there is no hyphen in the filename (ie no authors) and will also fail if any of the authors have a hyphenated name.

I can't come up with a single regular expression that will cope with these exceptions - however you can solve the problem using regular expressions in a Script method....

match = item.name.match(/([^-]*(- |$))(.*)/);
title = match[1];
authors = match[3].replace(/,/g, " &");
return title + authors;





15/06-21 00:57 - edited 15/06-21 00:58