[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Making a substituion at the last occurance of a pattern

Posted on 2013-11-23
4
Medium Priority
?
298 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

656 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