|
Peters Flexible RenAmiNg Kit (PFrank) Support and Discussion Site for the PFrank File/Folder Renaming Tool (***NEW HOME PAGE*** "http://pfrank.atwebpages.com")
|
View previous topic :: View next topic |
Author |
Message |
Tillomar
Joined: 18 Oct 2010 Posts: 5
|
Posted: Thu Oct 21, 2010 9:22 pm Post subject: Replacement operator shows unexpected behavior |
|
|
Hi *,
it's me once again. As I'm just starting my way into RegEx, there are lot's of things that can and do go wrong (I already stopped counting). But for some of those not-so-rare occasions I did not find a reason, lest a solution. I hope someone can help me...
I continue to fix file names of my ebook collection. I try to put the names of the author(s) in front of the filename, and the name parts in the right order. The following RegEx are designed to extract the last name and put it into the front position; this has to be done for several authors in sequence whose names are delimited by ampersand characters:
s1: Code: | (?ix) (?=^[^,]+&.*$)(?!.*[jJ][rR]\.?$)^((\w+\.?\s+)+)(\w+)(\s*&.*)?$ |
r1:
s2: Code: | (?ix) (?=^[^&]*&[^,]+$)(?!.*[jJ][rR]\.?$)^([^&]*)&\s*((\w+\.?\s+)+)(\w+)(\s*[&-].*)?$ # swap name parts for 2nd of multiple authors (w/ optional title) |
r2:
(You may want to ignore the two assertions in front of the core RegEx as they should (hopefully) not be connected to the problem).
This is a test case: Code: | Tom Clancy & Martin Greenberg - Powerplays 01 - Politika.pdf |
The result after the first S/R is: Code: | Clancy, Tom & Martin Greenberg - Powerplays 01 - Politika.pdf |
After the second: Code: | Clancy, Tom & Greenberg, Martin - Powerplays 01 - Politika.pdf |
(I don't mind the duplicate spaces -- they're filtered afterwards.)
This is as I expect.
But this test case does not work: Code: | Tom Clancy & Martin Greenberg |
(It's simply missing the optional title part -- needed for folders.)
The result after the first S/R is: Code: | Clancy, Tom & Martin Greenberg |
After the second S/R the result is empty!
But if I change r2 to:
r2:
(replacement of optional last term removed)
then I get the expected result: Code: | Clancy, Tom & Greenberg, Martin |
What am I missing??? _________________ So long,
Tillomar |
|
Back to top |
|
|
admin Site Admin
Joined: 09 Mar 2007 Posts: 448 Location: Canada
|
Posted: Fri Oct 22, 2010 5:24 am Post subject: |
|
|
Hi,
I'm not sure at the moment why it happens but it seems to be because you are specifying a group reference for a group that does not exist.
To work around the problem you could try the following (if you cut and paste the following info then do not include the single quotes):
Row: 1
Search: '^(\S+\s+)(\S+\s*)&\s*(\S+\s+)(\S+\s+)(\s*[-]\s*.*)'
Replace: '\2, \1 & \4, \3 \5'
Row: 2
Search: '^(\S+\s+)(\S+\s*)&\s*(\S+\s+)(\S+\s*)'
Replace: '\2, \1 & \4, \3'
The first search will only match strings that have the optional title (and will hence do the required replacement).
Strings that do not have the optional title will not be matched byk the first row but will be matched by the second row search and replaced accordingly.
I don't think you need to use the lookahead sequences that you had in your examples (unless I am missing something).
Hope that helps,
Peter. |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|