正規表現は、テキストを処理するための強力で柔軟で効率的な方法を提供します。 正規表現の広範なパターンマッチング表記により、大量のテキストをすばやく解析して次のことが可能になります。
- 特定の文字パターンを見つけます。
- テキストを検証して、事前定義されたパターン (メール アドレスなど) と一致することを確認します。
- テキスト部分文字列の抽出、編集、置換、または削除を行う
- レポートを生成するために、抽出された文字列をコレクションに追加します。
文字列を扱う、または大きなテキスト ブロックを解析する多くのアプリケーションでは、正規表現は不可欠なツールです。
正規表現のしくみ
正規表現を使用したテキスト処理の中心となるのは、.NET の System.Text.RegularExpressions.Regex オブジェクトによって表される正規表現エンジンです。 少なくとも、正規表現を使用してテキストを処理するには、正規表現エンジンに次の 2 つの情報を提供する必要があります。
テキスト内で識別する正規表現パターン。
.NET では、正規表現パターンは特殊な構文または言語によって定義されます。これは Perl 5 正規表現と互換性があり、右から左への一致など、いくつかの追加機能を追加します。 詳細については、「 正規表現言語 - クイック リファレンス」を参照してください。
正規表現パターンを解析するテキスト。
Regex クラスのメソッドを使用すると、次の操作を実行できます。
Regex.IsMatch メソッドを呼び出して、入力テキストで正規表現パターンが発生するかどうかを判断します。 テキストの検証に IsMatch メソッドを使用する例については、「 方法: 文字列が有効な電子メール形式であることを確認する」を参照してください。
Regex.Matchメソッドまたは Regex.Matches メソッドを呼び出して、正規表現パターンに一致するテキストの 1 つまたはすべての出現箇所を取得します。 前者のメソッドは、一致するテキストに関する情報を提供する System.Text.RegularExpressions.Match オブジェクトを返します。 後者は、解析されたテキストで見つかった一致ごとに 1 つのSystem.Text.RegularExpressions.Match オブジェクトを含むMatchCollection オブジェクトを返します。
Regex.Replace メソッドを呼び出して、正規表現パターンに一致するテキストを置き換えます。 Replaceメソッドを使用して日付形式を変更し、文字列から無効な文字を削除する例については、「方法: 文字列から無効な文字を削除する」および「例: 日付形式を変更する」を参照してください。
正規表現オブジェクト モデルの概要については、「 正規表現オブジェクト モデル」を参照してください。
正規表現言語の詳細については、「 正規表現言語 - クイック リファレンス 」を参照するか、次のいずれかのパンフレットをダウンロードして印刷します。
正規表現の例
String クラスには、大きな文字列内のリテラル文字列を検索する場合に使用できる文字列検索および置換メソッドが含まれています。 正規表現は、次の例に示すように、大きな文字列で複数の部分文字列のいずれかを検索する場合、または文字列内のパターンを識別する場合に最も便利です。
Warnung
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($"{match.Value} (duplicates '{match.Groups[1].Value}') at position {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? これは" です。 ただし、間に句読点があるため、重複として識別されません。
例 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 {matches.Count} matches.");
// 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 {expenses[expenses.Count - 1]:C2}.");
else
Console.WriteLine($"The expenses total {total:C2}.");
}
}
// 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]+)?)
を動的に構築します。 この正規表現パターンは、次のように解釈できます。
パターン | 解釈 |
---|---|
\$ |
入力文字列でドル記号 ($ ) が 1 回出現する箇所を探します。 正規表現パターン文字列には、ドル記号が正規表現アンカーとしてではなくリテラルに解釈されることを示す円記号が含まれています。
$ シンボルは、正規表現エンジンが文字列の末尾で一致を開始しようとする必要があることを示します。 現在のカルチャの通貨記号が正規表現シンボルとして誤って解釈されないように、この例では、 Regex.Escape メソッドを呼び出して文字をエスケープします。 |
\s* |
空白文字が 0 回以上出現する箇所を探します。 |
[-+]? |
正符号または負符号の 0 個または 1 回の出現を探します。 |
([0-9]{0,3}(,[0-9]{3})*(\.[0-9]+)?) |
外側のかっこは、この式をキャプチャ グループまたは部分式として定義します。 一致が見つかった場合、一致する文字列のこの部分に関する情報は、Match.Groups プロパティによって返されるGroupCollection オブジェクトの 2 番目のGroup オブジェクトから取得できます。 コレクション内の最初の要素は、一致全体を表します。 |
[0-9]{0,3} |
0 ~ 9 の 10 進数字が 0 から 3 回出現することを探します。 |
(,[0-9]{3})* |
グループ区切り記号の後に 3 桁の 10 進数字が続く、0 個以上の出現箇所を探します。 |
\. |
小数点区切り記号が 1 回出現する箇所を探します。 |
[0-9]+ |
1 桁以上の 10 進数を探します。 |
(\.[0-9]+)? |
小数点区切り記号が 0 個または 1 回出現し、その後に少なくとも 1 つの 10 進数字が続く箇所を探します。 |
各サブパターンが入力文字列で見つかった場合、一致が成功し、一致に関する情報を含む Match オブジェクトが MatchCollection オブジェクトに追加されます。
関連資料
タイトル | 説明 |
---|---|
正規表現言語 - クイック リファレンス | 正規表現の定義に使用できる文字、演算子、およびコンストラクトのセットに関する情報を提供します。 |
正規表現のオブジェクト モデル | 正規表現クラスの使用方法を示す情報とコード例を提供します。 |
正規表現の動作の詳細 | .NET 正規表現の機能と動作に関する情報を提供します。 |
Visual Studio で正規表現を使用する |
リファレンス
.NET