?
Solved

Making a substituion at the last occurance of a pattern

Posted on 2013-11-23
4
Medium Priority
?
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
We are witnesses that everyone is saying that our children shouldn't "play" with a technology because it is dangerous. This article is going to prove that they are wrong.
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…

765 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