From: Len Blanks <ltb@haruspex.net>
To: help-gnu-emacs@gnu.org
Subject: Troubles in Regular Expression Paradise
Date: Tue, 13 May 2014 21:18:58 -0500 [thread overview]
Message-ID: <m2vbt9qezx.fsf@haruspex.net> (raw)
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
next reply other threads:[~2014-05-14 2:18 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-05-14 2:18 Len Blanks [this message]
2014-05-14 12:40 ` Troubles in Regular Expression Paradise Nicolas Richard
2014-05-14 22:07 ` Len Blanks
[not found] <mailman.1280.1400034904.1147.help-gnu-emacs@gnu.org>
2014-05-14 2:56 ` Joost Kremers
2014-05-14 3:13 ` Len Blanks
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m2vbt9qezx.fsf@haruspex.net \
--to=ltb@haruspex.net \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).