all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Troubles in Regular Expression Paradise
@ 2014-05-14  2:18 Len Blanks
  2014-05-14 12:40 ` Nicolas Richard
  0 siblings, 1 reply; 5+ messages in thread
From: Len Blanks @ 2014-05-14  2:18 UTC (permalink / raw)
  To: help-gnu-emacs

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



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-05-14 22:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.1280.1400034904.1147.help-gnu-emacs@gnu.org>
2014-05-14  2:56 ` Troubles in Regular Expression Paradise Joost Kremers
2014-05-14  3:13   ` Len Blanks
2014-05-14  2:18 Len Blanks
2014-05-14 12:40 ` Nicolas Richard
2014-05-14 22:07   ` Len Blanks

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.