all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Easy regexp issue
@ 2007-01-18 18:32 HS
  2007-01-18 19:32 ` Joost Kremers
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: HS @ 2007-01-18 18:32 UTC (permalink / raw)


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")

And if I try:
(replace-regexp-in-string "/" "\" "c:/emacs/lisp/")
I get:
Debugger entered--Lisp error: (scan-error "Unbalanced parentheses" 302
1)

So, I don't see any other options!
I know the problems is there because I replace for another string then
it works:
(replace-regexp-in-string "/" "x" "c:/emacs/lisp/")
"c:xemacsxlispx"

So, any ideas?

Thanks again!
HS

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

* Re: Easy regexp issue
  2007-01-18 18:32 Easy regexp issue HS
@ 2007-01-18 19:32 ` Joost Kremers
  2007-01-19  7:28 ` Kevin Rodgers
       [not found] ` <mailman.3295.1169191738.2155.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 7+ messages in thread
From: Joost Kremers @ 2007-01-18 19:32 UTC (permalink / raw)


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)

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

* Re: Easy regexp issue
  2007-01-18 18:32 Easy regexp issue HS
  2007-01-18 19:32 ` Joost Kremers
@ 2007-01-19  7:28 ` Kevin Rodgers
       [not found] ` <mailman.3295.1169191738.2155.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 7+ messages in thread
From: Kevin Rodgers @ 2007-01-19  7:28 UTC (permalink / raw)


HS wrote:
> 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")
> 
> And if I try:
> (replace-regexp-in-string "/" "\" "c:/emacs/lisp/")
> I get:
> Debugger entered--Lisp error: (scan-error "Unbalanced parentheses" 302
> 1)
> 
> So, I don't see any other options!
> I know the problems is there because I replace for another string then
> it works:
> (replace-regexp-in-string "/" "x" "c:/emacs/lisp/")
> "c:xemacsxlispx"

See the LITERAL argument in the doc string for replace-regexp-in-string:

(replace-regexp-in-string "/" "\\" "c:/emacs/lisp/" nil t)

-- 
Kevin Rodgers
Denver, Colorado, USA

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

* Re: Easy regexp issue
       [not found] ` <mailman.3295.1169191738.2155.help-gnu-emacs@gnu.org>
@ 2007-01-19  7:49   ` Barry Margolin
  2007-01-19 13:29     ` HS
  2007-01-20  6:09     ` Kevin Rodgers
  0 siblings, 2 replies; 7+ messages in thread
From: Barry Margolin @ 2007-01-19  7:49 UTC (permalink / raw)


In article <mailman.3295.1169191738.2155.help-gnu-emacs@gnu.org>,
 Kevin Rodgers <kevin.d.rodgers@gmail.com> wrote:

> HS wrote:
> > 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")
> > 
> > And if I try:
> > (replace-regexp-in-string "/" "\" "c:/emacs/lisp/")
> > I get:
> > Debugger entered--Lisp error: (scan-error "Unbalanced parentheses" 302
> > 1)
> > 
> > So, I don't see any other options!
> > I know the problems is there because I replace for another string then
> > it works:
> > (replace-regexp-in-string "/" "x" "c:/emacs/lisp/")
> > "c:xemacsxlispx"
> 
> See the LITERAL argument in the doc string for replace-regexp-in-string:
> 
> (replace-regexp-in-string "/" "\\" "c:/emacs/lisp/" nil t)

Or

(replace-regexp-in-string "/" "\\\\" "c:/emacs/lisp/")

This is necessary because backslash is an escape character at two 
levels: Lisp strings and regexp replacement strings (e.g. the sequence 
\1 in the replacement represents a group matched in the regexp).  So the 
replacement string needs to be \\, and you need to escape each \ in the 
string.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

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

* Re: Easy regexp issue
  2007-01-19  7:49   ` Barry Margolin
@ 2007-01-19 13:29     ` HS
  2007-01-22 19:26       ` Robert Thorpe
  2007-01-20  6:09     ` Kevin Rodgers
  1 sibling, 1 reply; 7+ messages in thread
From: HS @ 2007-01-19 13:29 UTC (permalink / raw)


Thanks everyone! The problem was solved!
I wanted it for this:

(defun open-this-folder ()
  "Opens current buffer folder with explorer"
  (interactive)
  (open-folder default-directory))

(defun open-folder (folder)
  "Opens a folder with explorer (winxp only)"
  (interactive)
  (shell-command
   (concat "explorer " (replace-regexp-in-string "/" "\\\\\\\\"
folder)))
  (message (concat "Opening folder: " folder)))

Barry Margolin escreveu:

> In article <mailman.3295.1169191738.2155.help-gnu-emacs@gnu.org>,
>  Kevin Rodgers <kevin.d.rodgers@gmail.com> wrote:
>
> > HS wrote:
> > > 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")
> > >
> > > And if I try:
> > > (replace-regexp-in-string "/" "\" "c:/emacs/lisp/")
> > > I get:
> > > Debugger entered--Lisp error: (scan-error "Unbalanced parentheses" 302
> > > 1)
> > >
> > > So, I don't see any other options!
> > > I know the problems is there because I replace for another string then
> > > it works:
> > > (replace-regexp-in-string "/" "x" "c:/emacs/lisp/")
> > > "c:xemacsxlispx"
> >
> > See the LITERAL argument in the doc string for replace-regexp-in-string:
> >
> > (replace-regexp-in-string "/" "\\" "c:/emacs/lisp/" nil t)
>
> Or
>
> (replace-regexp-in-string "/" "\\\\" "c:/emacs/lisp/")
>
> This is necessary because backslash is an escape character at two
> levels: Lisp strings and regexp replacement strings (e.g. the sequence
> \1 in the replacement represents a group matched in the regexp).  So the
> replacement string needs to be \\, and you need to escape each \ in the
> string.
>
> --
> Barry Margolin, barmar@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
> *** PLEASE don't copy me on replies, I'll read them in the group ***

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

* Re: Easy regexp issue
  2007-01-19  7:49   ` Barry Margolin
  2007-01-19 13:29     ` HS
@ 2007-01-20  6:09     ` Kevin Rodgers
  1 sibling, 0 replies; 7+ messages in thread
From: Kevin Rodgers @ 2007-01-20  6:09 UTC (permalink / raw)


Barry Margolin wrote:
> In article <mailman.3295.1169191738.2155.help-gnu-emacs@gnu.org>,
>  Kevin Rodgers <kevin.d.rodgers@gmail.com> wrote:
>> See the LITERAL argument in the doc string for replace-regexp-in-string:
>>
>> (replace-regexp-in-string "/" "\\" "c:/emacs/lisp/" nil t)
> 
> Or
> 
> (replace-regexp-in-string "/" "\\\\" "c:/emacs/lisp/")
> 
> This is necessary because backslash is an escape character at two 
> levels: Lisp strings and regexp replacement strings (e.g. the sequence 
> \1 in the replacement represents a group matched in the regexp).  So the 
> replacement string needs to be \\, and you need to escape each \ in the 
> string.

That was my point: If you don't need the backslash to be interpreted in
the replacement string as a submatch reference, specify a non-nil
LITERAL argument -- which should execute faster to boot.  Of course,
there's no way around the need to escape the backslash in the Lisp
string.

-- 
Kevin Rodgers
Denver, Colorado, USA

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

* Re: Easy regexp issue
  2007-01-19 13:29     ` HS
@ 2007-01-22 19:26       ` Robert Thorpe
  0 siblings, 0 replies; 7+ messages in thread
From: Robert Thorpe @ 2007-01-22 19:26 UTC (permalink / raw)


HS wrote:
> Thanks everyone! The problem was solved!
> I wanted it for this:

For future reference, when messing with this problem:
The function "princ" is useful, it prints things without lisp syntax
around them.  So, if you do (princ foo) you see what string a function
will recieve when sent the string foo.  Therefore, you can write foo in
lisp syntax and see what it means.

For example (princ "\\\\\\") =>\\\

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

end of thread, other threads:[~2007-01-22 19:26 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-01-18 18:32 Easy regexp issue HS
2007-01-18 19:32 ` Joost Kremers
2007-01-19  7:28 ` Kevin Rodgers
     [not found] ` <mailman.3295.1169191738.2155.help-gnu-emacs@gnu.org>
2007-01-19  7:49   ` Barry Margolin
2007-01-19 13:29     ` HS
2007-01-22 19:26       ` Robert Thorpe
2007-01-20  6:09     ` Kevin Rodgers

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.