Quantcast
Channel: C#タグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8895

Rubyの正規表現をC#で書いてみた

$
0
0

動機

かなり前にRubyを使ってたことがあり、Rubyで簡潔に書ける正規表現をC#で書いたらどうなるのかを書いてみたいと思った。
(C#好きですが、この記事でRuby→C#への乗り換えを推奨しているつもりはないですので、あしからず。)

やってみたこと

Ruby 正規表現のまとめ
で書かれている内容がいい感じなので、ほぼ同じ内容で、C#でやったらどうなるかを勝手に書いてみました。
コードはRubyより長くはなるけど、対応する方法はあると言えそう。

usingSystem;usingSystem.Text.RegularExpressions;classSamples{// $& 相当staticvoidSample1(){Console.WriteLine("--- Sample1 ---");Regexr=newRegex("C.");Matchm=r.Match("ABCDE");if(m.Success){// マッチしたらtrueConsole.WriteLine(m.Groups[0].Value);// $& "CD"}}// $1, $2, $3, ... 相当staticvoidSample2(){Console.WriteLine("--- Sample2 ---");Regexr=newRegex("(.)(.)(.)");Matchm=r.Match("ABC");if(m.Success){Console.WriteLine(m.Groups[1].Value);// $1 "A"Console.WriteLine(m.Groups[2].Value);// $2 "B"Console.WriteLine(m.Groups[3].Value);// $3 "C"}}// scan 相当staticvoidSample3(){Console.WriteLine("--- Sample3 ---");Regexr=newRegex(".a");MatchCollectionmc=r.Matches("abracatabra");foreach(Matchminmc){Console.WriteLine(m.Groups[0].Value);// "ra"// "ca"// "ta"// "ra"}}// sub 相当staticvoidSample4(){Console.WriteLine("--- Sample4 ---");stringstr="abc_def__g__hi";Regexr=newRegex("_+");Console.WriteLine(r.Replace(str," ",1));// 3個目のパラメータ(1)が回数// "abc def__g__hi"}// gsub 相当staticvoidSample5(){Console.WriteLine("--- Sample5 ---");stringstr="abc_def__g__hi";Regexr=newRegex("_+");Console.WriteLine(r.Replace(str," "));// gsub "abc def g hi"}// gsub 相当 その2staticvoidSample6(){Console.WriteLine("--- Sample6 ---");stringstr="abracatabra";Regexr=newRegex(".a");Console.WriteLine(r.Replace(str,"<$0>"));// "ab<ra><ca><ta>b<ra>"}// Regexp.escape 相当staticvoidSample7(){Console.WriteLine("--- Sample7 ---");Console.WriteLine(Regex.Escape("abc*def"));// "abc\*def"}// =~ 相当(index取得)staticvoidSample8(){Console.WriteLine("--- Sample8 ---");Regexr=newRegex("On");stringstr="RubyOnRails";Matchm=r.Match(str);if(m.Success){intindex=m.Groups[0].Captures[0].Index;// =~ 相当 (一致箇所のindexを返す)intlength=m.Groups[0].Captures[0].Length;Console.WriteLine("Index:"+index.ToString());Console.WriteLine("Length:"+length.ToString());Console.WriteLine(str.Substring(0,index));// $`Console.WriteLine(m.Groups[0].Value);// $&Console.WriteLine(str.Substring(index+length,str.Length-(index+length)));// $'}}[STAThread]staticvoidMain(){Sample1();Sample2();Sample3();Sample4();Sample5();Sample6();Sample7();Sample8();}}

ちなみにC#では、(C言語と違い)バックスラッシュ文字をエスケープする文字列記法 @""があるので、正規表現を使うときには、こっちを使うほうが間違えにくいです。
("hoge\\[foo"@"hoge\[foo"と書ける。)

実行結果

--- Sample1 ---
CD
--- Sample2 ---
A
B
C
--- Sample3 ---
ra
ca
ta
ra
--- Sample4 ---
abc def__g__hi
--- Sample5 ---
abc def g hi
--- Sample6 ---
ab<ra><ca><ta>b<ra>
--- Sample7 ---
abc\*def
--- Sample8 ---
Index:4
Length:2
Ruby
On
Rails

Viewing all articles
Browse latest Browse all 8895

Trending Articles