Solved

Making a substituion at the last occurance of a pattern

Posted on 2013-11-23
4
294 Views
Last Modified: 2013-11-23
I have a file that contain data formatted like this:

<product>
<isbn>0000000001</isbn>
<contributor>
<role>A01</role>
<author>Lee, Stan</author>
</contributor>
<contributor>
<role>A01</role>
<author>Steranko, Jim</author>
</contributor>
<contributor>
<role>A01</role>
<author>Adams, Neal</author>
</contributor>
<contributor>
<role>A01</role>
<author>Smith, Barry</author>
</contributor>
<random data 1>random data</random data 1>
<random data 2>random data</random data 2>
</product>

<product>
<isbn>0000000002</isbn>
<contributor>
<role>A01</role>
<author>Lee, Stan</author>
</contributor>
<contributor>
<role>A01</role>
<author>Steranko, Jim</author>
</contributor>
<random data 1>random data</random data 1>
<random data 2>random data</random data 2>
</product>

<product>
<isbn>0000000003</isbn>
<contributor>
<role>A01</role>
<author>Adams, Neal</author>
</contributor>
<random data 1>random data</random data 1>
<random data 2>random data</random data 2>
</product>

And I need to place the tag <XXXPLACEHOLDERXXX> at the end of the last occurrence of </contributor> for each record set ... since the <random data 1> could be anything, I can't base it on that.

So for example,

<product>
<isbn>0000000002</isbn>
<contributor>
<role>A01</role>
<author>Lee, Stan</author>
</contributor>
<contributor>
<role>A01</role>
<author>Steranko, Jim</author>
</contributor>
<random data 1>random data</random data 1>
<random data 2>random data</random data 2>
</product>

would need to be:

<product>
<isbn>0000000002</isbn>
<contributor>
<role>A01</role>
<author>Lee, Stan</author>
</contributor>
<contributor>
<role>A01</role>
<author>Steranko, Jim</author>
</contributor>
<XXXPLACEHOLDERXXX>
<random data 1>random data</random data 1>
<random data 2>random data</random data 2>
</product>

Though it isn't a big deal if its </contributor><XXXPLACEHOLDERXXX>, just as long as its right after the last </contributor> for each record set.
0
Comment
Question by:hadrons
4 Comments
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 39671593
See if this makes sense for you: http://www.myregextester.com/?r=b626bc6c
0
 
LVL 84

Expert Comment

by:ozo
ID: 39671596
perl -i.bak -pe 'BEGIN{$/="</product>"}s#(.*</contributor>)#$1\n<XXXPLACEHOLDERXXX>#s' file
0
 
LVL 27

Accepted Solution

by:
ddrudik earned 500 total points
ID: 39671617
match:
/(?s)<product>(?:(?!<\/product>).)*<\/contributor>\s*/

Open in new window

replace:
$&<XXXPLACEHOLDERXXX>

Open in new window

0
 

Author Closing Comment

by:hadrons
ID: 39671693
All the answers were helpful, but this was the best for my needs
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question