Solved

Help with an regular expression matching multiple lines

Posted on 2013-11-25
9
336 Views
Last Modified: 2013-11-26
I'm working on a file with a format somewhat like this:

<price>
<list>1</list>
<country>CAD</country>
</price>
<price>
<list>2</list>
<country>USD</country>
</price>
<price>
<list>3</list>
<country>CAD</country>
</price>

I need an expression that will delete the <price>..</price> composites that don't have USD in them.

The closest I was able to get was this:

$/="";
if (/<price>/../<\/price>/){s/<price>.*?<list>.*?<\/list>.*?<country>CAD<\/country>.*?<\/price>//s}

It would delete the top composite, but it would leave the other CAD on the bottom ... and the line isn't very robust because additional lines with different values just deletes everything.
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
9 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 400 total points
ID: 39676273
$_='
<price>
<list>1</list>
<country>CAD</country>
</price>
<price>
<list>2</list>
<country>USD</country>
</price>
<price>
<list>3</list>
<country>CAD</country>
</price>
';
s#<price>((?!USD).)*</price>##sg;
print;
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 39676303
Whenever processing XML, you should really, REALLY use an XML processing module - at least XML::Simple if not XML::LibXML, XML::Twig, XML::SAX (my current favorite) or one of the others.
0
 

Author Comment

by:hadrons
ID: 39676524
Actually wilcoxon, I am using XML:Twig, but I've found that i still have to re-edit the files because the data itself is so dirty and I haven't found a good way to prevent node A from printing if node B has a certain value.

ozo, the expression is very close to what I need, but the expression is taking too big a chunk of data because other data that's in between the price composites is being consolidated and deleted too.
0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 84

Expert Comment

by:ozo
ID: 39676527
Can you give an example of data for which the expression does not work, along with an example of what you would want the result to be?
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 100 total points
ID: 39676531
Try adding a ? character to ozo's pattern as follows:
s#<price>((?!USD).)*?</price>##sg;

Open in new window


Edit: On further thought, that probably won't help!
0
 

Author Comment

by:hadrons
ID: 39676632
ozo, here's a better example of what I was looking for (my first example wasn't very good):

The file starts like this;

<product>
<price>
<list>1</list>
<country>CAD</country>
</price>
</product>

<product>
<price>
<list>2</list>
<country>CAD</country>
</price>
<price>
<list>2</list>
<country>USD</country>
</price>
</product>

<product>
<price>
<list>2</list>
<country>USD</country>
</price>
<price>
<list>3</list>
<country>CAD</country>
</price>
</product>

And to end up with this:

<product>
</product>

<product>
<price>
<list>2</list>
<country>USD</country>
</price>
</product>

<product>
<price>
<list>2</list>
<country>USD</country>
</price>
</product>
0
 
LVL 9

Expert Comment

by:Derek Jensen
ID: 39678312
@Terry, don't doubt yourself! ;-) This seemed to work fine for me:

s#<price>((?!USD).+?)</price>##sg;
0
 
LVL 84

Expert Comment

by:ozo
ID: 39678363
Other than a small difference in white space, the modification in http:#a39676531 does seem to produce the desired result on the example of http:#a39676632
It may also be conceivable that
s#<price>((?!USD|</price>).)*</price>\s*##sg;
could work better in some circumstances.
0
 

Author Closing Comment

by:hadrons
ID: 39678388
Thank you all ... when I saw Terry's edited comment I didn't try using the non-greedy match, but it worked perfectly
0

Featured Post

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

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…
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.…
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…
Six Sigma Control Plans

617 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