LoginSignup
4

More than 5 years have passed since last update.

Vimの正規表現を使用してグループ毎に置換する方法

Posted at

Vimで正規表現を使用してグループ毎の置換をしたい時、Perl等の正規表現とは違って独特の
書き方をしないといけないので注意すること。 よく自分が忘れるのでメモを残した。

  1. (.*)と書けばいいところを、\(.*\)のように\(, )の前に加えなければならない。
  2. $1$2...は\1\2のように書かなければならない。

以下のコマンドを実行すると、その下の実行結果のようになる。

実行前
status=ステータス
dataList=データ一覧
deptid=ユーザの組織ID
userid=ユーザID
updateDate=更新日時
コマンド
:%s/\(.*\)=\(.*\)/hashMap.put("\1", "\2");/g
実行後
hashMap.put("status", "ステータス");
hashMap.put("dataList", "データ一覧");
hashMap.put("deptid", "ユーザの組織ID");
hashMap.put("userid", "ユーザID");
hashMap.put("updateDate", "更新日時");

参考

Vimで使える正規表現
http://archiva.jp/web/tool/vim_regexps.html

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4