[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Easy regexp issue
From: |
Joost Kremers |
Subject: |
Re: Easy regexp issue |
Date: |
18 Jan 2007 19:32:17 GMT |
User-agent: |
slrn/0.9.8.1 (Linux) |
HS wrote:
> Hello everyone!
>
> I'm finding this quite odd:
> I want to replace all slashes for backslashes in a string, but i'm
> having trouble!
>
> Running this:
> (replace-regexp-in-string "/" "\\" "c:/emacs/lisp/")
> I get:
> Debugger entered--Lisp error: (error "Invalid use of `\\' in
> replacement text")
try:
(replace-regexp-in-string "/" "\\\\" "c:/emacs/lisp")
in (the visual representation of) strings, a backslash is the escape
character, so when you want an actual backslash in a string, you must
escape it, i.e. write it as >>\\<<.
in regular expressions, the backslash is also an escape character, so that
if you want an actual backslash in a regexp, you have to escape it.
so, if you want to use an actual backslash in a regexp that is represented
as a string, you have to escape it with a backslash (for the regexp), and
then escape *both* of those backslashes with a backslash (for the string).
IOW, in your example, the regexp is "\\", which is a string representation
for >>\<<. so what you have is actually a single backslash regexp, which is
not a licit regexp.
--
Joost Kremers joostkremers@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)