ellem has asked for the wisdom of the Perl Monks concerning the following question:

I have a large iTunes XML file that I would like to change. An example is: <key>Album</key><string>1963 - The Lightning Fingers of Roy Clark</string> I would like to remove "1963 - " or "#### - " for all the instances (about 3658) of this.

I am trying various versions of s/(<key>Album<\/key><string>)\d+\s*-\s*/$1/ ; without much luck.

So the idea to me is open a file, identify the proper string and delete the offending "nnnn - " in some loop until there are no more to do and close the file. Maybe print some cheery - you're done now! type message.

Ideas, man/web pages, modules?

--
ellem@optonline.net
There's more than one way to do it, but only some of them actually work.

Replies are listed 'Best First'.
•Re: Delete from string through s///
by merlyn (Sage) on Aug 26, 2004 at 19:37 UTC
Re: Delete from string through s///
by ikegami (Pope) on Aug 26, 2004 at 20:09 UTC

    As provided, it works for me:

    #!perl -l $_ = '<key>Album</key><string>1963 - The Lightning Fingers of Roy Clar +k</string>'; s/(<key>Album<\/key><string>)\d+\s*-\s*/$1/ ; print; # outputs: <key>Album</key><string>The Lightning Fingers of Roy Clark< +/string>

    Are you trying to do multiple replaces at once? If so, you need need s///g;. If that's not it, could it be a character set issue? In any case, it's usually much better to use parsers than regexps for HTML and for XML.

Re: Delete from string through s///
by borisz (Canon) on Aug 27, 2004 at 00:04 UTC
    I recommend writing a sax filter. Based on XML::SAX::Base. Used by XML::SAX::Machines. Thats a really reliable solution.
    Boris
Re: Delete from string through s///
by johnnywang (Priest) on Aug 26, 2004 at 19:41 UTC
    will this work for you?
    s/(?<=<string>)\d+\s*-\s*//;
Re: Delete from string through s///
by ysth (Canon) on Aug 26, 2004 at 21:25 UTC
    I think you want the /g flag on the substitution to make it do all matches, not just the first. See perlop.
Re: Delete from string through s///
by Skeeve (Parson) on Aug 27, 2004 at 08:58 UTC
    Just a side note: AFAIK iTunes will simply rewrite your XML file. I tried changing the location of several mp3 files after I moved them to a different hard disk. iTunes simply ignored my corrections in it's XML file and rewrote the old content.

    So changing it for using in other files will be okay. Using the result in iTunes will most certainly not work.