Peters Flexible RenAmiNg Kit (PFrank) Forum Index Peters Flexible RenAmiNg Kit (PFrank)
Support and Discussion Site for the PFrank File/Folder Renaming Tool (***NEW HOME PAGE*** "http://pfrank.atwebpages.com")
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Replacement operator shows unexpected behavior

 
Post new topic   Reply to topic    Peters Flexible RenAmiNg Kit (PFrank) Forum Index -> General Support
View previous topic :: View next topic  
Author Message
Tillomar



Joined: 18 Oct 2010
Posts: 5

PostPosted: Thu Oct 21, 2010 9:22 pm    Post subject: Replacement operator shows unexpected behavior Reply with quote

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:
Code:
\3, \1\4


s2:
Code:
(?ix) (?=^[^&]*&[^,]+$)(?!.*[jJ][rR]\.?$)^([^&]*)&\s*((\w+\.?\s+)+)(\w+)(\s*[&-].*)?$   # swap name parts for 2nd of multiple authors (w/ optional title)

r2:
Code:
\1& \4, \2\5


(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:
Code:
\1& \4, \2

(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
View user's profile Send private message
admin
Site Admin


Joined: 09 Mar 2007
Posts: 448
Location: Canada

PostPosted: Fri Oct 22, 2010 5:24 am    Post subject: Reply with quote

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
View user's profile Send private message Send e-mail Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    Peters Flexible RenAmiNg Kit (PFrank) Forum Index -> General Support All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © phpBB Group. Hosted by phpBB.BizHat.com


Start Your Own Video Sharing Site

Free Web Hosting | Free Forum Hosting | FlashWebHost.com | Image Hosting | Photo Gallery | FreeMarriage.com

Powered by PhpBBweb.com, setup your forum now!
For Support, visit Forums.BizHat.com