C Sharpの基礎 - 正規表現

提供:MochiuWiki - SUSE, Electronic Circuit, PCB
ナビゲーションに移動 検索に移動

概要

C#において、Regexクラスの使用方法について記載する。

Regexクラスには、IsMatch、Match、Matches、Replace等のメソッドが存在する。
正規表現を使用して、入力文字列にマッチするかの判定や、マッチした文字列の置換等ができる。


IsMatch

C#では、IsMatchメソッドを使用して、パターンにマッチするか判定できる。

 using System.Text.RegularExpressions;
 
 class Sample
 {
    static void Main()
    {
       string str = ""Hello world."";
 
       // パターンにマッチするか判定
       bool bResult1 = Regex.IsMatch(str, ""Hello"");
       bool bResult2 = Regex.IsMatch(str, ""[a-z]*"");
       bool bResult3 = Regex.IsMatch(str, ""[0-9]+"");
 
       System.Console.WriteLine(bResult1);
       System.Console.WriteLine(bResult2);
       System.Console.WriteLine(bResult3);
 
       System.Console.ReadKey();
    }
 }
 
 // 出力
 True
 True
 False



Matchメソッド

C#では、Matchメソッドを使用して、検索文字列の先頭のパターンにマッチする文字列を抽出できる。

 using System.Text.RegularExpressions;
 
 class Sample
 {
    static void Main()
    {
       string str = ""Hello world."";
 
       // 最初にパターンにマッチする文字列の抽出
       Match matche1 = Regex.Match(str, ""[a-zA-Z]+"");
       Match matche2 = Regex.Match(str, ""[a-z]"");
 
       System.Console.WriteLine(matche1.Value);
       System.Console.WriteLine(matche2.Value);
 
       System.Console.ReadKey();
    }
 }
 
 // 出力
 Hello
 e


また、Matchメソッドに"RegexOptions.RightToLeft"を指定することで、
検索文字列の最後尾からパターンにマッチする文字列を抽出できる。

 using System.Text.RegularExpressions;
 
 class Sample
 {
    static void Main()
    {
       string str = ""Hello world."";
 
       // 最後にパターンにマッチする文字列の抽出
       Match matche1 = Regex.Match(str, ""[a-zA-Z]+"", RegexOptions.RightToLeft);
       Match matche2 = Regex.Match(str, ""[a-z]"", RegexOptions.RightToLeft);
 
       System.Console.WriteLine(matche1.Value);
       System.Console.WriteLine(matche2.Value);
 
       System.Console.ReadKey();
    }
 }
 
 // 出力
 world
 d



Matchesメソッド

C#では、Matchesメソッドを使用して、パターンにマッチする文字列を抽出できる。

 using System.Text.RegularExpressions;
 
 class Sample
 {
    static void Main()
    {
       string str = ""Hello world."";
 
       // パターンにマッチする文字列の抽出
       MatchCollection matche = Regex.Matches(str, ""[a-e]"");
 
       // マッチした文字列の表示
       foreach (Match m in matche)
       {
          System.Console.WriteLine(m.Value);
       }
      
       System.Console.ReadKey();
    }
 }
 
 // 出力
 e
 d



Replaceメソッド

C#では、Replaceメソッドを使用して、パターンにマッチする文字列を置換できる。

 using System.Text.RegularExpressions;
 
 class Sample
 {
    static void Main()
    {
       string str = ""Hello world."";
 
       Regex regex = new Regex(""o"");
 
       // パターンにマッチする文字列をすべて置換
       string result = regex.Replace(str, ""X"");
 
       System.Console.WriteLine(result);
       System.Console.ReadKey();
    }
 }
 
 //出力
 HellX wXrld.


また、パターンの最初にマッチする文字列のみを置換することもできる。

 using System.Text.RegularExpressions;
 
 class Sample
 {
    static void Main()
    {
       string str = ""Hello world."";
 
       Regex regex = new Regex(""o"");
 
       // パターンにマッチする最初の文字列を置換
       string result = regex.Replace(str, ""X"", 1);
 
       System.Console.WriteLine(result);
       System.Console.ReadKey();
    }
 }
 
 // 出力
 HellX world.



応用

パターンの最後尾にマッチする文字列のみを置換することもできる。

 using System.Text.RegularExpressions;
 
 class Sample
 {
    static void Main()
    {
       string str = ""Hello world."";
 
       // パターンにマッチする最後の文字列を置換
       string result = ReplaceLast(str, ""o"", ""X"");
 
       System.Console.WriteLine(result);
       System.Console.ReadKey();
    }
 
    public static string ReplaceLast(string str, string target, string replace)
    {
       int index = str.LastIndexOf(target);
 
       if(index == -1)
       {
          return str;
       }
 
       string result = str.Remove(index, target.Length).Insert(index, replace);
            
       return result;
    }
 }
 
 // 出力
 Hello wXrld.