0

I'd like to replace all occurrences of \w \w with \w\\\w in a string, where the \w parts stay the same before and after the replacement, e.g.

[.A foobar] [.B baz]

should result in

[.A\\foobar] [.B\\baz]

it should also work with

[.A' foobar] [.B -baz]

=>

[.A'\\foobar] [.B\\-baz]

From jlf @ #emacs

(while (string-match "\(.*\w\) \(\w\)" str)
  (setq str (concat (match-string 1 str) "\\" (match-string 2 str))))
2
  • If your edit answers your question you should post it as an answer and accept it if no one posts a better solution. Commented Oct 10, 2012 at 1:04
  • It's not a complete solution, just a hint. Commented Oct 10, 2012 at 4:20

2 Answers 2

2

(replace-regexp-in-string " \\_<\\w+\\_>" (lambda (x) (concat "\\\\s" (substring x 1))) mystring)

Sign up to request clarification or add additional context in comments.

5 Comments

Nice try, but (replace-regexp-in-string "\_<\w+\_>" (lambda (x) (concat "\\" x)) "[.CP foo]") => "[.CP foo]".
Oh, escape hell. Works almost as expected. "[.CP \\foo]" is the result, but "[.CP\\foo]" would be better.
regexp starts with a whitespace now
Would you know how to include - as possible part of the word?
not sure what you are asking for, maybe open a new question giving the example
1

This may be a little bit more versatile:

(defun replace-between-words (input replacement &optional preserve)
  "Replaces white space between two words with REPLACEMENT
if REPLACEMENT is a character, and PRESERVE is not NIL, then
that character is duplicated as many times as there are white spaces
between words. If PRESERVE is NIL, then only one character is
inserted.
If REPLACEMENT is a string and PRESERVE is not NIL, then it is rolled
into the available white space, otherwise the entire replacement string
is insterted."
  (with-output-to-string
    (let ((match "*") (replaced t)
          (white-count 0)
          seen-start seen-end current whites)
      (dotimes (i (length input))
        (setf current (aref input i)
              (aref match 0) current)
        (cond
         ((string-match "\\w" match)
          (if seen-end
              (progn
                (if (stringp replacement)
                    (if preserve
                        (dotimes (j white-count)
                          (write-char
                           (aref replacement
                                 (mod j (length replacement )))))
                      (princ replacement))
                  (if preserve
                      (dotimes (j white-count)
                        (write-char replacement))
                    (write-char replacement)))
                (setq seen-end nil))
            (setq seen-start t))
          (setq whites nil white-count 0)
          (write-char current))
         ((member current '(?\ ?\t ?\n ?\r))
          (if seen-start
              (if seen-end
                  (progn
                    (setq whites (cons current whites))
                    (incf white-count))
                (setq seen-end t white-count 1 whites (list ?\ )))
            (write-char current)))
         (t (when (> white-count 0)
              (princ (coerce whites 'string))
              (setq white-count 0 whites nil))
             (write-char current)
            (setq seen-end nil seen-start nil)))))))

(replace-between-words "[.A foobar]    [.B   baz]" ?\/)
"[.A/foobar]    [.B/baz]"

(replace-between-words "[.A foobar]    [.B   baz]" "-=*=-" t)
"[.A-foobar]    [.B-=*baz]"

(replace-between-words "[.A foobar]    [.B   baz]" "-=*=-")
"[.A-=*=-foobar]    [.B-=*=-baz]"

(replace-between-words "[.A foobar]    [.B   baz]" ?\/ t)
"[.A/foobar]    [.B///baz]"

(replace-between-words "[.CP [.TP [.NP" ?\/ t)
"[.CP [.TP [.NP"

And may be eventually even faster :)

2 Comments

This does not preserve other whitespace - [.CP [.TP [.NP is converted to [.CP[.TP[.NP.
There's a c floating around - I replaced it with white-count.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.