0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Visual Studio 2015 C#] 置換の正規表現がかなり便利

Posted at

正規表現を用いての置換なんてものは、今更(常識)なんでしょうが、
投稿があまり見当たらなかったので、中途半端な記事ですがあえて投稿します。

使い方は簡単。
置換する際に、検索する文字列を入力する欄(上側)に「.*」を用いるだけ。

※置換文字列を入力する、すぐ下の星マークボタン(Alt+E)を押して、正規表現を有効にする必要あり。
また、()や{}など、下記構文で用いられている文字列は「\」(¥の半角)を用いてエスケープするも必要あり。

※その他の構文は以下参照
https://msdn.microsoft.com/ja-jp/library/ae5bf541(v=vs.100).aspx

例えば、

sample.cs
Test a = new Test();
Test b = new Test();

を以下のように変えたいときに

sample.cs
var t = new List<Test>{ new Test(), new Test() };

「Test .* = new」を検索文字列として、空白に置換すれば前半部分が消えます。
そして、tを宣言して、「;」を「,」に置き換えれば、出来上がりというわけです。
(空白を「,」に置換してもよいです)

0
1
0

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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?