*目次 [#b8cbb30c]
#contents

*概要 [#p792bbcd]
emacsの正規表現の置換は置換するときに計算できて賢い感じ

**先頭を大文字にする置換 [#w97d5acb]

一致した箇所を先頭を大文字にする関数に渡すことができます。

 ;;置換前
 foo
 bar
 baz
 ;;置換後
 Foo
 Bar
 Baz

置換前のfooからbazまでを選択してから下記のコマンドを実行します。
 M-x replace-regexp
 Replace regexp: \(\w+\)
 Replace regexp with: \,(capitalize \1)

**カウントアップさせる置換 [#mc406463]
 ;;置換前
 <h1>foo</h1>
 <h2>bar</h2>
 <h3>baz</h3>
 ;;置換後
 <h2>foo</h2>
 <h3>bar</h3>
 <h4>baz</h4>

同様に選択して、下記のコマンドを実行します。
 M-x replace-regexp
 Replace regexp: <\(/?\)h\([0-9]\)>
 Replace regexp with: <\1h\,(1+ \#2)>



***参考 [#xacb4c49]
http://dminor11th.blogspot.jp/2011/02/emacslisp.html


*キャメルケースとスネークケースを相互変換 [#de0b3f50]
 (defun camel-to-snake (s &optional do-upcase)
 (let ((case-fold-search nil))
 (funcall (if do-upcase 'upcase 'downcase)

 (replace-regexp-in-string
 "\\([A-Z]\\)" "_\\1"
 (concat (downcase (substring s 0 1)) (substring s 1))))))
 
 (defun snake-to-camel (s &optional capitalize)
 (let ((case-fold-search nil))
 (replace-regexp-in-string
 "_\\([a-zA-Z]\\)"
 (lambda (s2) (upcase (substring s2 1)))
 (concat (if capitalize "_" "") s))))


**情報源 [#ibf31f12]
http://recyclebin5385.blog13.fc2.com/blog-category-4.html

*選択した文字列を変数に入れる例 [#n0c3bec5]
 (defun get-select-text ()
   (interactive)
   (setq selected-text  
 	(buffer-substring  
 	 (region-beginning)  
 	 (region-end)))
   (insert selected-text))


トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS