Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help with an regular expression matching multiple lines

Posted on 2013-11-25
9
Medium Priority
?
340 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 85

Accepted Solution

by:
ozo earned 1600 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 27

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 85

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 400 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 85

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

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

971 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