[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Troubles in Regular Expression Paradise
From: |
Joost Kremers |
Subject: |
Re: Troubles in Regular Expression Paradise |
Date: |
14 May 2014 02:56:28 GMT |
User-agent: |
slrn/pre1.0.0-18 (Linux) |
Len Blanks wrote:
> (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.
You can only use such substitution operators in functions that are aware
of them. Normal string-handling functions are not, you'll need something
like match-string.
--
Joost Kremers joostkremers@fastmail.fm
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)