[Top][All Lists]

[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 
on my iTunes - specifically the artist, name of the tune and the CD in appears 
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>
                <comments><![CDATA[ Song ID: 228676960]]></comments>
                <composer><![CDATA[Alabama Shakes]]></composer>

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 
header in emails and usenet posts:

(defun now-playing (xml-file)
  ;;  (interactive "FFile: ")
    (insert-file-contents xml-file)
    (goto-char 1)
    (when (re-search-forward (concat 
"<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 
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.

The two most common elements in the universe are hydrogen and
stupidity. -- Harlan Ellison

reply via email to

[Prev in Thread] Current Thread [Next in Thread]