Solved

Making a substituion at the last occurance of a pattern

Posted on 2013-11-23
4
293 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

777 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