# Unescape elisp string

I have a function which returns a file path, but the returned value has backslash excaped 'special' characters e.g.

"/home/fred/Documents/This\ file\ $$20200120$$.txt"

but the function where I want to use this value does not handle the string escaping and just wants the plain filename string i.e.

"/home/fred/Documents/This file (20200120).txt"

What would be the canonical way to do this in elisp?

My quick and dirty solution (which seems to work) is a simple function

(defun tx-parse-path (p)
(concat (reduce #'(lambda (acc l)
(if (not (= ?\\ l))
(cons l acc)
acc))
(reverse p)
:initial-value '())))


I suspect there is some built-in function, but have not found it. I don't need to worry about win32 compatibility (only use GNU Linux and macOS) and of course, the flaw with the brute force solution above is that it will strip legitimate '\' from a filename.

• Can you give us some details about the first function -- the one which returns the escaped string? How is that value generated? – phils Jan 24 '20 at 8:47
• An alternative solution would be adjusting the function you obtain the path from to not insert backslashes. – wasamasa Jan 24 '20 at 12:00
• I haven't dissected the code yet. The value actually comes from string property value. I've not yet gone into the code which generates the value and puts it into the string property. This isn't my code - it is part of a package I'm using which fails when the path name is used because of the 'escaped' characters. – Tim X Jan 24 '20 at 12:04
• Changing the function which generates the escaped value is not a preferred approach. This is not my code and I don't want to have to go through all the ways it is used to verify such changes won't have other unforeseen consequences. – Tim X Jan 24 '20 at 12:06
• You will need to establish how the escaped value was generated in the first place, so that you can confirm what the rules for reversing it are. For example, shell-quote-argument might do more than just add backslashes, depending on the input. – phils Jan 24 '20 at 13:53

(replace-regexp-in-string "\\\\\$$.\\|\n\$$" "\\1" STRING)

(replace-regexp-in-string (rx "\\" (group anything)) "\\1" STRING)