Solved

Making a substituion at the last occurance of a pattern

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

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now