Solved

Help with an regular expression matching multiple lines

Posted on 2013-11-25
9
328 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
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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
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…
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…

706 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

15 Experts available now in Live!

Get 1:1 Help Now