Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Mac OS X > Perl REGEX Help

Perl REGEX Help
Thread Tools
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Jul 21, 2003, 05:23 PM
 
I'm just parsing throught the XML file that one gets from exporting a playlist from iTunes.

See the snipplet below. How do I extract any kinds of characters between the <string> tags? As is, the regex skips over white spaces and other not alphanumeric characters.



Code:
$stuff="playlist1.xml"; open STUFF, $stuff or die "Cannot open $stuff for read :$!"; while (<STUFF>) { if(/<key>Artist<\/key><string>(\w+)<\/string>/){ print "Line $. is : $_"; print "$1\n" } }


I can probably hammer this with a SAX parser but I want to learn how to do it by just using regular expressions.

Thanks!
(Last edited by DaGuy; Jul 21, 2003 at 05:44 PM. )
     
Mac Enthusiast
Join Date: Jun 2000
Location: New Jersey, USA
Status: Offline
Reply With Quote
Jul 21, 2003, 05:52 PM
 
The \w in the regex matches word characters; if you want to match anything, then just use dot (.). Dot matches any character.

In other words:

Code:
if(/<key>Artist<\/key><string>(.*)<\/string>/){
And I'm using ".*" instead of ".+" just in case there's no artist, and there's nothing in between <string> and </string>. In that case your match will just be a null string.

Er, maybe you don't want to match in that situation, in which case go back to ".+".
     
DaGuy  (op)
Senior User
Join Date: Oct 2000
Location: Lawrence, KS
Status: Offline
Reply With Quote
Jul 21, 2003, 07:25 PM
 
Awesome! Thanks very much.

     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 08:20 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2