Solved

Help with an regular expression matching multiple lines

Posted on 2013-11-25
9
329 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for u…
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…
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…

867 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

22 Experts available now in Live!

Get 1:1 Help Now