0

文字列内のすべての出現箇所\w \wを置換したいのですが、置換の前後で部品が同じままです。\w\\\w\w

[.A foobar] [.B baz]

結果として

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

それも動作するはずです

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

=>

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

jlf @ #emacs から

(while (string-match "\(.*\w\) \(\w\)" str)
  (setq str (concat (match-string 1 str) "\\" (match-string 2 str))))
4

2 に答える 2

2

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

于 2012-10-12T06:39:05.123 に答える
1

これはもう少し用途が広いかもしれません:

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

そして、最終的にはさらに高速になる可能性があります:)

于 2012-10-10T03:15:12.313 に答える