Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to replace aa_bcd with aaBcd

0 views
Skip to first unread message

SHIIBA Michiharu

unread,
Jun 22, 2002, 6:14:13 AM6/22/02
to
椎葉と言います。

query-replace-regexp に関する質問です。

(defun foo ()
(interactive)
(query-replace-regexp "\\(\\w\\)_\\(\\w\\)" "\\1\\2")))

のような関数を用意して実行することにより、バッファ内にある

aa_bcd, foo_bar

のような文字列を

aabcd, foobar

にできますが、同時に _ の後の1文字を大文字に変換して

aaBcd, fooBar

のようになるようにするには、先の関数 foo をどのように書き変えれば
よいでしょうか。

よろしくお願いします。

--
椎葉 充晴 (Michiharu CBAR)

Katsumi Yamaoka

unread,
Jun 26, 2002, 9:57:34 PM6/26/02
to
>>>>> In <x5u1nvy...@civilnet.kuciv.kyoto-u.ac.jp>
>>>>> SHIIBA Michiharu <shi...@civilnet.kuciv.kyoto-u.ac.jp> wrote:

椎葉さん> query-replace-regexp に関する質問です。

椎葉さん> (defun foo ()
椎葉さん> (interactive)
椎葉さん> (query-replace-regexp "\\(\\w\\)_\\(\\w\\)" "\\1\\2")))

[...]

椎葉さん> aaBcd, fooBar

椎葉さん> のようになるようにするには、先の関数 foo をどのように書き変
椎葉さん> えればよいでしょうか。

query-replace-regexp だけではたぶん無理です。こんな感じでしょうか?

(defun foo2 ()
(interactive)
(let (old new)
(while (re-search-forward "\\(\\w+\\)_\\(\\w+\\)" nil t)
(setq old (match-string 0)
new (concat (match-string 1)
(capitalize (match-string 2))))
(if (y-or-n-p (concat "Query replacing \"" old
"\" with \"" new "\" "))
(replace-match new)))
(message "done")))
--
山岡

SHIIBA Michiharu

unread,
Jun 30, 2002, 10:30:29 PM6/30/02
to
山岡様

ありがとうございました。

山岡様> query-replace-regexp だけではたぶん無理です。こんな感じでしょうか?

山岡様> (defun foo2 ()
山岡様> (interactive)
山岡様> (let (old new)
山岡様> (while (re-search-forward "\\(\\w+\\)_\\(\\w+\\)" nil t)
山岡様> (setq old (match-string 0)
山岡様> new (concat (match-string 1)
山岡様> (capitalize (match-string 2))))
山岡様> (if (y-or-n-p (concat "Query replacing \"" old
山岡様> "\" with \"" new "\" "))
山岡様> (replace-match new)))
山岡様> (message "done")))

foo_bar_aho などに対応するために、

(replace-match new)


(progn
(replace-match new)
(backward-char 1))

として使うことにしました。どうもありがとうございました。

Keiichi Suzuki

unread,
Jul 1, 2002, 3:57:53 AM7/1/02
to
>>>>> Message-Id: <x5hejkc...@civilnet.kuciv.kyoto-u.ac.jp> の記事で、
>>>>> "椎葉" == SHIIBA Michiharu <shi...@civilnet.kuciv.kyoto-u.ac.jp>さま曰く...

(defun y-or-n-p-with-replace-highlight (beg end form &rest args)
(unwind-protect
(progn
(pop-to-buffer (current-buffer))
(replace-highlight beg end)
(y-or-n-p (apply 'format form args)))
(replace-dehighlight)))

何てものをあらかじめ定義しておいて

山岡様> (if (y-or-n-p (concat "Query replacing \"" old
山岡様> "\" with \"" new "\" "))

.の代りに

(if (y-or-n-p-with-replace-highlight
(match-beginning 0) (match-end 0)
"Quely replaceing \"%s\" with \"%s\" "
old new)

などとするとちょっとだけ幸せかも知れません。

--
鈴木圭一 / kei...@nanap.org
PGP finger print (DH/DSS)
0B32 B37E 6DE9 3BC1 68A4 4089 7AAF 2B03 ECBD 614B

0 new messages