[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Troubles in Regular Expression Paradise
From: |
Len Blanks |
Subject: |
Troubles in Regular Expression Paradise |
Date: |
Tue, 13 May 2014 21:18:58 -0500 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (darwin) |
I'm trying to parse an xml file containing information for a song "currently
playing"
on my iTunes - specifically the artist, name of the tune and the CD in appears
on.
Here's an example of the file:
<?xml version="1.0" encoding="UTF-8"?>
<now_playing playing="1" timestamp="2014-05-13T16:55:31Z">
<song timestamp="2014-05-13T16:55:30Z">
<title><![CDATA[Hold On]]></title>
<artist><![CDATA[Alabama Shakes]]></artist>
<album><![CDATA[Boys & Girls]]></album>
<genre><![CDATA[Alternative Rock]]></genre>
<kind>MPEG audio file</kind>
<track>1</track>
<numTracks>12</numTracks>
<year>2012</year>
<comments><![CDATA[Amazon.com Song ID: 228676960]]></comments>
<time>226</time>
<bitrate>249</bitrate>
<disc>1</disc>
<numDiscs>1</numDiscs>
<compilation>No</compilation>
<urlAmazon>http://www.amazon.com/Boys-Girls-Alabama-Shakes/dp/B0074MZSWW%3FSubscriptionId%3D03AKJ1J6S0FY8K0WRER2%26tag%3Dnowplaplu-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB0074MZSWW</urlAmazon>
<urlApple/>
<imageSmall>http://ecx.images-amazon.com/images/I/61P1-X1QkoL._SL75_.jpg</imageSmall>
<image>http://ecx.images-amazon.com/images/I/61P1-X1QkoL._SL160_.jpg</image>
<imageLarge>http://ecx.images-amazon.com/images/I/61P1-X1QkoL.jpg</imageLarge>
<composer><![CDATA[Alabama Shakes]]></composer>
<grouping><![CDATA[]]></grouping>
<file><![CDATA[]]></file>
<artworkID>c53ba0b14763278952c3fb1f8ea1da1f</artworkID>
</song>
</now_playing>
and here is a function I had hoped would strip the relevant fields and return a
string in
the form: "_artist_'s _title_ from the CD _album_" to be inserted in a
X-NOW-PLAYING:
header in emails and usenet posts:
(defun now-playing (xml-file)
;; (interactive "FFile: ")
(with-temp-buffer
(insert-file-contents xml-file)
(goto-char 1)
(when (re-search-forward (concat
"<title><!\\[CDATA\\[\\([^\\]+\\)\\]></title>"
"[\0-\377[:nonascii:]]*"
"<artist><!\\[CDATA\\[\\([^\\]+\\)\\]></artist>"
"[\0-\377[:nonascii:]]*"
"<album><!\\[CDATA\\[\\([^\\]+\\)\\]></album>") nil t)
(concat "\\2"
(if (string= (downcase (substring "\\2" -2 -1)) "s") "'" "'s")
" \\1 from the CD \\3"))))
(message (now-playing "/tmp/now_playing.xml")) ;; test now-playing
The regular expression was built and tested using re-build and it works well in
matching
including the groupings \\( ... \\), which re-build colours quite nicely. But
I seem to have done
something really foolish since referencing \\1, \\2 and \\3 fail, so they don't
seem to be
properly set by the groupings in the re.
The function returns "\2's \1 from the CD \3".
I'm sure the problem is something foolish, but I would really like to know what
i did.
--
Len
The two most common elements in the universe are hydrogen and
stupidity. -- Harlan Ellison
- Troubles in Regular Expression Paradise,
Len Blanks <=