.NET の正規表現

正規表現を使用すると、強力、柔軟、そして効率的な方法でテキストを処理できます。 正規表現によるパターン一致の広範な表記法を使用すると、大量のテキストをすばやく解析し、次のことを行うことができます。

  • 特定の文字パターンを検索する
  • テキストを検証して、定義済みのパターン (電子メール アドレスなど) と一致することを確実にする
  • テキスト部分文字列の抽出、編集、置換、または削除を行う
  • 抽出された文字列をコレクションに追加して、レポートを生成する

文字列処理や大量のテキストを解析する多くのアプリケーションにとって、正規表現は欠くことのできないツールです。

正規表現のしくみ

正規表現を使ったテキスト処理の最も重要な部分は、.NET の System.Text.RegularExpressions.Regex オブジェクトによって表される正規表現エンジンです。 正規表現を使ったテキスト処理では、正規表現エンジンに対し、最低でも次の 2 つの情報を与える必要があります。

  • テキストを識別する正規表現パターン。

    .NET では、正規表現のパターンが特殊な構文または言語で定義されます。この構文または言語には、Perl 5 の正規表現と互換性があるほか、右から左への一致処理など、いくつかの機能が追加されています。 詳細については、「正規表現言語 - クイック リファレンス」をご覧ください。

  • 正規表現パターンの解析対象となるテキスト。

Regex クラスのメソッドを使用すると、次のような処理を実行できます。

正規表現のオブジェクト モデルの概要については、「正規表現のオブジェクト モデル」をご覧ください。

正規表現の言語について詳しくは、「正規表現言語 - クイック リファレンス」を参照するか、次の資料のいずれかをダウンロードして印刷してください。

正規表現の例

String クラスには、文字列内のリテラル文字列を検索する際に使用できる文字列の検索メソッドと置換メソッドが含まれています。 正規表現は、次の例に示すように、文字列内の部分文字列のいずれかを検索する場合、または文字列内のパターンを識別する場合に最も役立ちます。

警告

System.Text.RegularExpressions を使用して信頼できない入力を処理するときは、タイムアウトを渡します。 悪意のあるユーザーが RegularExpressions に入力を提供して、サービス拒否攻撃を行う可能性があります。 RegularExpressions を使用する ASP.NET Core フレームワーク API は、タイムアウトを渡します。

ヒント

System.Web.RegularExpressions 名前空間には、正規表現オブジェクトがたくさん含まれています。このオブジェクトは、HTML、XML、ASP.NET 文書の文字列を解析する事前定義済み正規表現パターンを実装します。 たとえば、TagRegex クラスは文字列の開始タグを識別します。CommentRegex クラスは文字列の ASP.NET コメントを識別します。

例 1: 部分文字列の置換

氏名に敬称 (Mr.、Mrs.、Miss、または Ms.) が付いている場合がある名前が、宛先リストに含まれるとします。 リストから封筒ラベルを生成するときにタイトルを含めないとします。 その場合、次の例に示すように、正規表現を使用してタイトルを削除できます。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "(Mr\\.? |Mrs\\.? |Miss |Ms\\.? )";
      string[] names = { "Mr. Henry Hunt", "Ms. Sara Samuels",
                         "Abraham Adams", "Ms. Nicole Norris" };
      foreach (string name in names)
         Console.WriteLine(Regex.Replace(name, pattern, String.Empty));
   }
}
// The example displays the following output:
//    Henry Hunt
//    Sara Samuels
//    Abraham Adams
//    Nicole Norris
Imports System.Text.RegularExpressions

Module Example
    Public Sub Main()
        Dim pattern As String = "(Mr\.? |Mrs\.? |Miss |Ms\.? )"
        Dim names() As String = {"Mr. Henry Hunt", "Ms. Sara Samuels", _
                                  "Abraham Adams", "Ms. Nicole Norris"}
        For Each name As String In names
            Console.WriteLine(Regex.Replace(name, pattern, String.Empty))
        Next
    End Sub
End Module
' The example displays the following output:
'    Henry Hunt
'    Sara Samuels
'    Abraham Adams
'    Nicole Norris

正規表現パターン (Mr\.? |Mrs\.? |Miss |Ms\.? ) は、"Mr "、"Mr. "、"Mrs "、"Mrs. "、"Miss "、"Ms "、または "Ms. " の出現と一致します。 Regex.Replace メソッドを呼び出すと、一致する文字列が String.Empty に置き換えられます。つまり、元の文字列から削除されます。

例 2:重複する単語の識別

記述者が単語を誤って重複入力するというエラーがよくあります。 次の例に示すように、正規表現を使用して重複する単語を識別します。

using System;
using System.Text.RegularExpressions;

public class Class1
{
   public static void Main()
   {
      string pattern = @"\b(\w+?)\s\1\b";
      string input = "This this is a nice day. What about this? This tastes good. I saw a a dog.";
      foreach (Match match in Regex.Matches(input, pattern, RegexOptions.IgnoreCase))
         Console.WriteLine("{0} (duplicates '{1}') at position {2}",
                           match.Value, match.Groups[1].Value, match.Index);
   }
}
// The example displays the following output:
//       This this (duplicates 'This') at position 0
//       a a (duplicates 'a') at position 66
Imports System.Text.RegularExpressions

Module modMain
    Public Sub Main()
        Dim pattern As String = "\b(\w+?)\s\1\b"
        Dim input As String = "This this is a nice day. What about this? This tastes good. I saw a a dog."
        For Each match As Match In Regex.Matches(input, pattern, RegexOptions.IgnoreCase)
            Console.WriteLine("{0} (duplicates '{1}') at position {2}", _
                              match.Value, match.Groups(1).Value, match.Index)
        Next
    End Sub
End Module
' The example displays the following output:
'       This this (duplicates 'This') at position 0
'       a a (duplicates 'a') at position 66

正規表現パターン \b(\w+?)\s\1\b は、次のように解釈できます。

パターン 解釈
\b ワード境界から開始します。
(\w+?) 1 つ以上 (ただし、できるだけ少ない文字数) の単語文字と一致します。 同時に、\1 というグループを形成します。
\s 空白文字と一致します。
\1 \1 という名前のグループと等しい部分文字列と一致します。
\b ワード境界に一致します。

Regex.Matches メソッドは、正規表現オプションを RegexOptions.IgnoreCase に設定して呼び出されます。 したがって、照合操作では大文字と小文字が区別されず、この例では部分文字列 "This this" が重複として識別されます。

入力文字列には部分文字列 "this? This" が含まれています。 ただし、句読点が介在するので、重複として識別されません。

例 3:カルチャに依存した正規表現の動的な構築

ここでは、正規表現による強力なテキスト処理と、.NET の柔軟なグローバリゼーション機能を組み合わせて使用する例を紹介します。 この例では、システムの現在のカルチャで用いられている通貨値の形式を調べるために、NumberFormatInfo オブジェクトが使用されています。 さらに、その情報を基に、テキストから通貨値を抽出する正規表現を動的に構築します。 検出された一致ごとに、数値文字列のみを含んだサブグループを抽出し、Decimal 値に変換して、通算の合計を計算します。

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      // Define text to be parsed.
      string input = "Office expenses on 2/13/2008:\n" +
                     "Paper (500 sheets)                      $3.95\n" +
                     "Pencils (box of 10)                     $1.00\n" +
                     "Pens (box of 10)                        $4.49\n" +
                     "Erasers                                 $2.19\n" +
                     "Ink jet printer                        $69.95\n\n" +
                     "Total Expenses                        $ 81.58\n";

      // Get current culture's NumberFormatInfo object.
      NumberFormatInfo nfi = CultureInfo.CurrentCulture.NumberFormat;
      // Assign needed property values to variables.
      string currencySymbol = nfi.CurrencySymbol;
      bool symbolPrecedesIfPositive = nfi.CurrencyPositivePattern % 2 == 0;
      string groupSeparator = nfi.CurrencyGroupSeparator;
      string decimalSeparator = nfi.CurrencyDecimalSeparator;

      // Form regular expression pattern.
      string pattern = Regex.Escape( symbolPrecedesIfPositive ? currencySymbol : "") +
                       @"\s*[-+]?" + "([0-9]{0,3}(" + groupSeparator + "[0-9]{3})*(" +
                       Regex.Escape(decimalSeparator) + "[0-9]+)?)" +
                       (! symbolPrecedesIfPositive ? currencySymbol : "");
      Console.WriteLine( "The regular expression pattern is:");
      Console.WriteLine("   " + pattern);

      // Get text that matches regular expression pattern.
      MatchCollection matches = Regex.Matches(input, pattern,
                                              RegexOptions.IgnorePatternWhitespace);
      Console.WriteLine("Found {0} matches.", matches.Count);

      // Get numeric string, convert it to a value, and add it to List object.
      List<decimal> expenses = new List<Decimal>();

      foreach (Match match in matches)
         expenses.Add(Decimal.Parse(match.Groups[1].Value));

      // Determine whether total is present and if present, whether it is correct.
      decimal total = 0;
      foreach (decimal value in expenses)
         total += value;

      if (total / 2 == expenses[expenses.Count - 1])
         Console.WriteLine("The expenses total {0:C2}.", expenses[expenses.Count - 1]);
      else
         Console.WriteLine("The expenses total {0:C2}.", total);
   }
}
// The example displays the following output:
//       The regular expression pattern is:
//          \$\s*[-+]?([0-9]{0,3}(,[0-9]{3})*(\.[0-9]+)?)
//       Found 6 matches.
//       The expenses total $81.58.
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Text.RegularExpressions

Public Module Example
    Public Sub Main()
        ' Define text to be parsed.
        Dim input As String = "Office expenses on 2/13/2008:" + vbCrLf + _
                              "Paper (500 sheets)                      $3.95" + vbCrLf + _
                              "Pencils (box of 10)                     $1.00" + vbCrLf + _
                              "Pens (box of 10)                        $4.49" + vbCrLf + _
                              "Erasers                                 $2.19" + vbCrLf + _
                              "Ink jet printer                        $69.95" + vbCrLf + vbCrLf + _
                              "Total Expenses                        $ 81.58" + vbCrLf
        ' Get current culture's NumberFormatInfo object.
        Dim nfi As NumberFormatInfo = CultureInfo.CurrentCulture.NumberFormat
        ' Assign needed property values to variables.
        Dim currencySymbol As String = nfi.CurrencySymbol
        Dim symbolPrecedesIfPositive As Boolean = CBool(nfi.CurrencyPositivePattern Mod 2 = 0)
        Dim groupSeparator As String = nfi.CurrencyGroupSeparator
        Dim decimalSeparator As String = nfi.CurrencyDecimalSeparator

        ' Form regular expression pattern.
        Dim pattern As String = Regex.Escape(CStr(IIf(symbolPrecedesIfPositive, currencySymbol, ""))) + _
                                "\s*[-+]?" + "([0-9]{0,3}(" + groupSeparator + "[0-9]{3})*(" + _
                                Regex.Escape(decimalSeparator) + "[0-9]+)?)" + _
                                CStr(IIf(Not symbolPrecedesIfPositive, currencySymbol, ""))
        Console.WriteLine("The regular expression pattern is: ")
        Console.WriteLine("   " + pattern)

        ' Get text that matches regular expression pattern.
        Dim matches As MatchCollection = Regex.Matches(input, pattern, RegexOptions.IgnorePatternWhitespace)
        Console.WriteLine("Found {0} matches. ", matches.Count)

        ' Get numeric string, convert it to a value, and add it to List object.
        Dim expenses As New List(Of Decimal)

        For Each match As Match In matches
            expenses.Add(Decimal.Parse(match.Groups.Item(1).Value))
        Next

        ' Determine whether total is present and if present, whether it is correct.
        Dim total As Decimal
        For Each value As Decimal In expenses
            total += value
        Next

        If total / 2 = expenses(expenses.Count - 1) Then
            Console.WriteLine("The expenses total {0:C2}.", expenses(expenses.Count - 1))
        Else
            Console.WriteLine("The expenses total {0:C2}.", total)
        End If
    End Sub
End Module
' The example displays the following output:
'       The regular expression pattern is:
'          \$\s*[-+]?([0-9]{0,3}(,[0-9]{3})*(\.[0-9]+)?)
'       Found 6 matches.
'       The expenses total $81.58.

現在 "英語 - 米国" (en-US) カルチャが使用されているコンピューターでは、\$\s*[-+]?([0-9]{0,3}(,[0-9]{3})*(\.[0-9]+)?) という正規表現が動的に作成されます。 この正規表現パターンは、次のように解釈できます。

パターン 解釈
\$ 入力文字列に含まれる単一のドル記号 ($) を検索します。 この正規表現パターン文字列に使用されている円記号は、ドル記号を正規表現のアンカーではなく、文字として扱うことを意味します。 $ 記号のみの場合、正規表現エンジンが文字列の最後で一致の開始を試みる必要があることを示します。 現在のカルチャの通貨記号が正規表現記号として解釈されるのを防ぐため、例では、Regex.Escape メソッドを呼び出して文字をエスケープしています。
\s* 空白文字の 0 回以上の繰り返しを検索します。
[-+]? 正の符号または負の符号の 0 回または 1 回の繰り返しを検索します。
([0-9]{0,3}(,[0-9]{3})*(\.[0-9]+)?) 外側のかっこによってこの式がキャプチャ グループまたは部分式として定義されます。 一致が見つかった場合、その一致した文字列の、この部分に関する情報が、Group プロパティから返された GroupCollection オブジェクトの 2 つ目の Match.Groups オブジェクトから取得できます コレクションの 1 つ目の要素は、一致した文字列全体を表します。
[0-9]{0,3} 10 進数字 (0 ~ 9) の 0 回以上、3 回以下の繰り返しを検索します。
(,[0-9]{3})* 桁区切り記号と 3 桁の 10 進数字の 0 回以上の繰り返しを検索します。
\. 単一の小数点を検索します。
[0-9]+ 10 進数字の 1 回以上の繰り返しを検索します。
(\.[0-9]+)? 小数点と 1 桁以上の数字の 0 回または 1 回の繰り返しを検索します。

各サブパターンが入力文字列内に見つかると一致と判断され、その一致に関する情報を含んだ Match オブジェクトが MatchCollection オブジェクトに追加されます。

Title 説明
正規表現言語 - クイック リファレンス 正規表現を定義するために使う一連の文字、演算子、および構成体について説明します。
正規表現のオブジェクト モデル 正規表現クラスの使用方法について詳しく説明し、コード例を示します。
正規表現の動作の詳細 .NET の正規表現の機能と動作について説明します。
Visual Studio での正規表現の使用

関連項目