RegexOptions 列挙型
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
正規表現オプションを設定するために使用する列挙値を提供します。
この列挙体は、メンバー値のビットごとの組み合わせをサポートしています。
public enum class RegexOptions
[System.Flags]
public enum RegexOptions
[<System.Flags>]
type RegexOptions =
Public Enum RegexOptions
- 継承
- 属性
フィールド
Compiled | 8 | 正規表現が解釈されるのではなく、MSIL コードにコンパイルされることを指定します。 コンパイルされた正規表現を使用すると、初期化処理に時間を要しますが、実行時のパフォーマンスは向上します。 この値は、Options メソッドを呼び出すときには CompileToAssembly(RegexCompilationInfo[], AssemblyName) プロパティに割り当てないでください。 詳細については、「正規表現のオプション」の記事の「コンパイルされた正規表現」を参照してください。 |
CultureInvariant | 512 | 言語の違いが無視されるように指定します。 詳細については、「正規表現のオプション」の記事の「インバリアント カルチャを使用した比較」を参照してください。 |
ECMAScript | 256 | 式の ECMAScript 準拠の動作を有効にします。 この値は、必ず IgnoreCase、Multiline、Compiled の各値と組み合わせて使用します。 この値を他の値と組み合わせて使用すると、例外が発生します。 ECMAScript オプションの詳細については、「正規表現のオプション」の記事の「ECMAScript 一致の動作」を参照してください。 |
ExplicitCapture | 4 | 明示的に名前または番号を指定された (?<name>...) の形式のグループだけが有効なキャプチャであることを指定します。これにより、不恰好な表現 (?:...) を使用しなくても、名前のないかっこが非キャプチャ グループとして機能するようになります。詳細については、「正規表現のオプション」の記事の「明示的なキャプチャのみ」を参照してください。 |
IgnoreCase | 1 | 検索時に大文字と小文字を区別しないことを指定します。 詳細については、「正規表現のオプション」の記事の「大文字と小文字を区別しない一致」を参照してください。 |
IgnorePatternWhitespace | 32 | パターンからエスケープが解除された空白を削除し、# でマークされたコメントを有効にします。 ただしこの値は、個々の正規表現の言語要素の開始を示す文字クラス、数値の量指定子、またはトークンに影響を与えることも、これらの中の空白を削除することもありません。 詳細については、「正規表現のオプション」の記事の「空白を無視」を参照してください。 |
Multiline | 2 | 複数行モードを指定します。 ^ と $ の意味を変更して、文字列全体の先頭と末尾だけでなく、任意の行の先頭と末尾にもそれぞれが一致するようにします。 詳細については、トピック「正規表現のオプション」記事の「複数行モード」を参照してください。 |
NonBacktracking | 1024 | バックトラッキングを回避し、入力の長さの線形時間処理を保証するアプローチを使用して、照合を有効にします。 |
None | 0 | オプションが何も設定されないことを指定します。 正規表現エンジンの既定の動作の詳細については、「正規表現のオプション」の記事の「既定のオプション」を参照してください。 |
RightToLeft | 64 | 検索が左から右ではなく右から左になされるように指定します。 詳細については、「正規表現のオプション」の記事の「右から左モード」を参照してください。 |
Singleline | 16 | 単一行モードを指定します。 \n 以外の任意の文字ではなく、すべての文字と一致するようにピリオド (.) の意味を変更します。 詳細については、「正規表現のオプション」の記事の「単一行モード」を参照してください。 |
例
次の例では、テキスト内の繰り返しの単語を識別するが、異なる RegexOptions
値を使用してインスタンス化される 2 つの正規表現を定義します。 最初の正規表現では大文字と小文字が区別されません。case は、単語が前の単語と同じかどうかを判断するときに無視されます。 2 番目の正規表現では大文字と小文字が区別されます。単語は、重複と見なされるためには、前の単語の大文字と小文字が完全に一致している必要があります。
using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main ()
{
// Define a case-insensitive regular expression for repeated words.
Regex rxInsensitive = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
// Define a case-sensitive regular expression for repeated words.
Regex rxSensitive = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
RegexOptions.Compiled);
// Define a test string.
string text = "The the quick brown fox fox jumps over the lazy dog dog.";
// Find matches using case-insensitive regular expression.
MatchCollection matches = rxInsensitive.Matches(text);
// Report the number of matches found.
Console.WriteLine("{0} matches found in:\n {1}",
matches.Count,
text);
// Report on each match.
foreach (Match match in matches)
{
GroupCollection groups = match.Groups;
Console.WriteLine("'{0}' repeated at positions {1} and {2}",
groups["word"].Value,
groups[0].Index,
groups[1].Index);
}
Console.WriteLine();
// Find matches using case-sensitive regular expression.
matches = rxSensitive.Matches(text);
// Report the number of matches found.
Console.WriteLine("{0} matches found in:\n {1}",
matches.Count,
text);
// Report on each match.
foreach (Match match in matches)
{
GroupCollection groups = match.Groups;
Console.WriteLine("'{0}' repeated at positions {1} and {2}",
groups["word"].Value,
groups[0].Index,
groups[1].Index);
}
}
}
// The example produces the following output to the console:
// 3 matches found in:
// The the quick brown fox fox jumps over the lazy dog dog.
// 'The' repeated at positions 0 and 4
// 'fox' repeated at positions 20 and 25
// 'dog' repeated at positions 50 and 54
//
// 2 matches found in:
// The the quick brown fox fox jumps over the lazy dog dog.
// 'fox' repeated at positions 20 and 25
// 'dog' repeated at positions 50 and 54
Imports System.Text.RegularExpressions
Public Module Test
Public Sub Main()
' Define a case-insensitive regular expression for repeated words.
Dim rxInsensitive As New Regex("\b(?<word>\w+)\s+(\k<word>)\b", _
RegexOptions.Compiled Or RegexOptions.IgnoreCase)
' Define a case-sensitive regular expression for repeated words.
Dim rxSensitive As New Regex("\b(?<word>\w+)\s+(\k<word>)\b", _
RegexOptions.Compiled)
' Define a test string.
Dim text As String = "The the quick brown fox fox jumps over the lazy dog dog."
' Find matches using case-insensitive regular expression.
Dim matches As MatchCollection = rxInsensitive.Matches(text)
' Report the number of matches found.
Console.WriteLine("{0} matches found in:", matches.Count)
Console.WriteLine(" {0}", text)
' Report on each match.
For Each match As Match In matches
Dim groups As GroupCollection = match.Groups
Console.WriteLine("'{0}' repeated at positions {1} and {2}", _
groups.Item("word").Value, _
groups.Item(0).Index, _
groups.Item(1).Index)
Next
Console.WriteLine()
' Find matches using case-sensitive regular expression.
matches = rxSensitive.Matches(text)
' Report the number of matches found.
Console.WriteLine("{0} matches found in:", matches.Count)
Console.WriteLine(" {0}", text)
' Report on each match.
For Each match As Match In matches
Dim groups As GroupCollection = match.Groups
Console.WriteLine("'{0}' repeated at positions {1} and {2}", _
groups.Item("word").Value, _
groups.Item(0).Index, _
groups.Item(1).Index)
Next
Console.WriteLine()
End Sub
End Module
' The example produces the following output to the console:
' 3 matches found in:
' The the quick brown fox fox jumps over the lazy dog dog.
' 'The' repeated at positions 0 and 4
' 'fox' repeated at positions 20 and 25
' 'dog' repeated at positions 50 and 54
'
' 2 matches found in:
' The the quick brown fox fox jumps over the lazy dog dog.
' 'fox' repeated at positions 20 and 25
' 'dog' repeated at positions 50 and 54
注釈
値は RegexOptions
、 クラスの次のメンバー Regex にパラメーターとして指定できます。
Regex.Regex(String, RegexOptions)クラス コンストラクター。
Regex.Replace(String, String, String, RegexOptions) メソッドと Regex.Replace(String, String, MatchEvaluator, RegexOptions) メソッド。
値は RegexOptions
、コンストラクターの RegexCompilationInfo パラメーターとして指定することも、 プロパティに RegexCompilationInfo.Options 直接割り当てることもできます。 その後、結果 RegexCompilationInfo の オブジェクトが メソッドの呼び出し Regex.CompileToAssembly で使用されます。
列挙型のRegexOptions
メンバーによって提供されるいくつかのオプション (特に、その ExplicitCapture
、IgnoreCase
Multiline
、および Singleline
メンバー) は、代わりに正規表現パターンでインライン オプション文字を使用して提供できます。 詳細については、「 正規表現オプション」を参照してください。