Solved

Help with an regular expression matching multiple lines

Posted on 2013-11-25
9
333 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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…

830 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