RegexCompilationInfo 构造函数
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
初始化 RegexCompilationInfo 类的新实例。
重载
RegexCompilationInfo(String, RegexOptions, String, String, Boolean) |
初始化 RegexCompilationInfo 类的新实例,该类包含有关要在程序集内包括的正则表达式的信息。 |
RegexCompilationInfo(String, RegexOptions, String, String, Boolean, TimeSpan) |
初始化 RegexCompilationInfo 类的新实例,该类包含有关要在程序集内包括的指定的超时值的信息。 |
RegexCompilationInfo(String, RegexOptions, String, String, Boolean)
- Source:
- RegexCompilationInfo.cs
- Source:
- RegexCompilationInfo.cs
- Source:
- RegexCompilationInfo.cs
初始化 RegexCompilationInfo 类的新实例,该类包含有关要在程序集内包括的正则表达式的信息。
public:
RegexCompilationInfo(System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options, System::String ^ name, System::String ^ fullnamespace, bool ispublic);
public RegexCompilationInfo (string pattern, System.Text.RegularExpressions.RegexOptions options, string name, string fullnamespace, bool ispublic);
new System.Text.RegularExpressions.RegexCompilationInfo : string * System.Text.RegularExpressions.RegexOptions * string * string * bool -> System.Text.RegularExpressions.RegexCompilationInfo
Public Sub New (pattern As String, options As RegexOptions, name As String, fullnamespace As String, ispublic As Boolean)
参数
- pattern
- String
要编译的正则表达式。
- options
- RegexOptions
编译正则表达式时使用的正则表达式选项。
- name
- String
表示已编译的正则表达式的类型名称。
- fullnamespace
- String
新类型所属的命名空间。
- ispublic
- Boolean
若要使所编译的正则表达式对公共可见,则为 true
;否则,为 false
。
例外
name
为 Empty。
示例
以下示例通过两个步骤创建并使用已编译的正则表达式。
在第一步中,编译并执行以下代码示例。
RegexCompilationInfo代码示例中的构造函数定义编译的正则表达式。 执行代码的结果是一个名为 FishRegex.dll 的程序集,其中包含名为 的 FishRegex
已编译正则表达式类型。
// This code example demonstrates the RegexCompilationInfo constructor
// and the Regex.CompileToAssembly() method.
// compile: csc genFishRegex.cs
namespace MyApp
{
using System;
using System.Reflection;
using System.Text.RegularExpressions;
class GenFishRegEx
{
public static void Main()
{
// Pattern = Group matches one or more word characters,
// one or more white space characters,
// group matches the string "fish".
string pat = @"(\w+)\s+(fish)";
// Create the compilation information.
// Case-insensitive matching; type name = "FishRegex";
// namespace = "MyApp"; type is public.
RegexCompilationInfo rci = new RegexCompilationInfo(
pat, RegexOptions.IgnoreCase,
"FishRegex", "MyApp", true);
// Setup to compile.
AssemblyName an = new AssemblyName();
an.Name = "FishRegex";
RegexCompilationInfo[] rciList = { rci };
// Compile the regular expression.
Regex.CompileToAssembly(rciList, an);
}
}
}
' This code example demonstrates the RegexCompilationInfo constructor
' and the Regex.CompileToAssembly() method.
' compile: csc genFishRegex.cs
Imports System.Reflection
Imports System.Text.RegularExpressions
Class GenFishRegEx
Public Shared Sub Main()
' Pattern = Group matches one or more word characters,
' one or more white space characters,
' group matches the string "fish".
Dim pat As String = "(\w+)\s+(fish)"
' Create the compilation information.
' Case-insensitive matching; type name = "FishRegex";
' namespace = "MyApp"; type is public.
Dim rci As New RegexCompilationInfo(pat, RegexOptions.IgnoreCase, _
"FishRegex", "MyApp", True)
' Setup to compile.
Dim an As New AssemblyName()
an.Name = "FishRegex"
Dim rciList As RegexCompilationInfo() = New RegexCompilationInfo() { rci }
' Compile the regular expression.
Regex.CompileToAssembly(rciList, an)
End Sub
End Class
第二步使用对 FishRegex.dll 的引用编译以下代码示例,然后运行生成的可执行文件。 可执行文件使用 FishRegex
类型匹配目标字符串,并在目标字符串中显示匹配项的匹配项、组、捕获组和索引位置。
// This code example demonstrates the RegexCompilationInfo constructor.
// Execute this code example after executing genFishRegex.exe.
// compile: csc /r:FishRegex.dll useFishRegex.cs
namespace MyApp
{
using System;
using System.Reflection;
using System.Text.RegularExpressions;
class UseFishRegEx
{
public static void Main()
{
// Match against the following target string.
string targetString = "One fish two fish red fish blue fish";
int matchCount = 0;
FishRegex f = new FishRegex();
// Display the target string.
Console.WriteLine("\nInput string = \"" + targetString + "\"");
// Display each match, capture group, capture, and match position.
foreach (Match m in f.Matches(targetString))
{
Console.WriteLine("\nMatch(" + (++matchCount) + ")");
for (int i = 1; i <= 2; i++)
{
Group g = m.Groups[i];
Console.WriteLine("Group(" + i + ") = \"" + g + "\"");
CaptureCollection cc = g.Captures;
for (int j = 0; j < cc.Count; j++)
{
Capture c = cc[j];
System.Console.WriteLine(
"Capture(" + j + ") = \"" + c + "\", Position = " + c.Index);
}
}
}
}
}
}
/*
This code example produces the following results:
Input string = "One fish two fish red fish blue fish"
Match(1)
Group(1) = "One"
Capture(0) = "One", Position = 0
Group(2) = "fish"
Capture(0) = "fish", Position = 4
Match(2)
Group(1) = "two"
Capture(0) = "two", Position = 9
Group(2) = "fish"
Capture(0) = "fish", Position = 13
Match(3)
Group(1) = "red"
Capture(0) = "red", Position = 18
Group(2) = "fish"
Capture(0) = "fish", Position = 22
Match(4)
Group(1) = "blue"
Capture(0) = "blue", Position = 27
Group(2) = "fish"
Capture(0) = "fish", Position = 32
*/
' This code example demonstrates the RegexCompilationInfo constructor.
' Execute this code example after executing genFishRegex.exe.
' compile: vbc /r:FishRegex.dll useFishRegex.vb
Imports System.Reflection
Imports System.Text.RegularExpressions
Class UseFishRegEx
Public Shared Sub Main()
' Match against the following target string.
Dim targetString As String = "One fish two fish red fish blue fish"
Dim matchCount As Integer = 0
Dim f As New MyApp.FishRegex()
' Display the target string.
Console.WriteLine(vbLf & "Input string = """ & targetString & """")
' Display each match, capture group, capture, and match position.
Dim m As Match
For Each m In f.Matches(targetString)
matchCount = matchCount + 1
Console.WriteLine(vbLf & "Match(" & matchCount & ")")
Dim i As Integer
For i = 1 to 2
Dim g As Group = m.Groups(i)
Console.WriteLine("Group(" & i & ") = """ & g.ToString() & """")
Dim cc As CaptureCollection = g.Captures
Dim j As Integer
For j = 0 To cc.Count-1
Dim c As Capture = cc(j)
System.Console.WriteLine("Capture(" & j & ") = """ & c.ToString() & _
""", Position = " & c.Index)
Next j
Next i
Next m
End Sub
End Class
'
'This code example produces the following results:
'
'Input string = "One fish two fish red fish blue fish"
'
'Match(1)
'Group(1) = "One"
'Capture(0) = "One", Position = 0
'Group(2) = "fish"
'Capture(0) = "fish", Position = 4
'
'Match(2)
'Group(1) = "two"
'Capture(0) = "two", Position = 9
'Group(2) = "fish"
'Capture(0) = "fish", Position = 13
'
'Match(3)
'Group(1) = "red"
'Capture(0) = "red", Position = 18
'Group(2) = "fish"
'Capture(0) = "fish", Position = 22
'
'Match(4)
'Group(1) = "blue"
'Capture(0) = "blue", Position = 27
'Group(2) = "fish"
'Capture(0) = "fish", Position = 32
'
注解
构造函数的每个参数 RegexCompilationInfo(String, RegexOptions, String, String, Boolean) 直接对应于 类的 RegexCompilationInfo 一个属性。 由于所有属性都是可读/写的,因此也可以直接分配其值。
方法 CompileToAssembly 生成包含已编译正则表达式的程序集。 因此,不应将 指定 Compiled 为 的值之 options
一。
如果 ispublic
为 true
,则为编译的正则表达式类提供公共可访问性。 也就是说,可以从在任何程序集中执行的代码实例化它。 如果 ispublic
为 false
,则以 C#Friend
) 或 Visual Basic) 辅助功能 ( (提供internal
编译的正则表达式类。 也就是说,它只能从与正则表达式类在同一程序集中执行的代码实例化。
调用方说明
此构造函数创建一个已编译的正则表达式,该正则表达式使用创建它的应用程序域的默认超时值。 如果为应用程序域定义了超时值,则编译的正则表达式将使用 值 InfiniteMatchTimeout,这可以防止模式匹配操作超时。建议用于创建已编译正则表达式的构造函数是 RegexCompilationInfo(String, RegexOptions, String, String, Boolean, TimeSpan),这样就可以设置超时间隔。
适用于
RegexCompilationInfo(String, RegexOptions, String, String, Boolean, TimeSpan)
- Source:
- RegexCompilationInfo.cs
- Source:
- RegexCompilationInfo.cs
- Source:
- RegexCompilationInfo.cs
初始化 RegexCompilationInfo 类的新实例,该类包含有关要在程序集内包括的指定的超时值的信息。
public:
RegexCompilationInfo(System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options, System::String ^ name, System::String ^ fullnamespace, bool ispublic, TimeSpan matchTimeout);
public RegexCompilationInfo (string pattern, System.Text.RegularExpressions.RegexOptions options, string name, string fullnamespace, bool ispublic, TimeSpan matchTimeout);
new System.Text.RegularExpressions.RegexCompilationInfo : string * System.Text.RegularExpressions.RegexOptions * string * string * bool * TimeSpan -> System.Text.RegularExpressions.RegexCompilationInfo
Public Sub New (pattern As String, options As RegexOptions, name As String, fullnamespace As String, ispublic As Boolean, matchTimeout As TimeSpan)
参数
- pattern
- String
要编译的正则表达式。
- options
- RegexOptions
编译正则表达式时使用的正则表达式选项。
- name
- String
表示已编译的正则表达式的类型名称。
- fullnamespace
- String
新类型所属的命名空间。
- ispublic
- Boolean
若要使所编译的正则表达式对公共可见,则为 true
;否则,为 false
。
- matchTimeout
- TimeSpan
正则表达式的默认超时间隔。
例外
name
为 Empty。
matchTimeout
为负、零或大于 24 天左右。
示例
以下示例定义一个名为 DuplicateChars
的单个编译正则表达式,该正则表达式标识输入字符串中同一字符的两个或更多个匹配项。 编译的正则表达式的默认超时为 2 秒。 执行该示例时,它会创建一个名为 RegexLib.dll 的类库,其中包含编译的正则表达式。
using System;
using System.Reflection;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
// Match two or more occurrences of the same character.
string pattern = @"(\w)\1+";
// Use case-insensitive matching.
var rci = new RegexCompilationInfo(pattern, RegexOptions.IgnoreCase,
"DuplicateChars", "CustomRegexes",
true, TimeSpan.FromSeconds(2));
// Define an assembly to contain the compiled regular expression.
var an = new AssemblyName();
an.Name = "RegexLib";
RegexCompilationInfo[] rciList = { rci };
// Compile the regular expression and create the assembly.
Regex.CompileToAssembly(rciList, an);
}
}
Imports System.Reflection
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
' Match two or more occurrences of the same character.
Dim pattern As String = "(\w)\1+"
' Use case-insensitive matching.
Dim rci As New RegexCompilationInfo(pattern, RegexOptions.IgnoreCase,
"DuplicateChars", "CustomRegexes",
True, TimeSpan.FromSeconds(2))
' Define an assembly to contain the compiled regular expression.
Dim an As New AssemblyName()
an.Name = "RegexLib"
Dim rciList As RegexCompilationInfo() = New RegexCompilationInfo() { rci }
' Compile the regular expression and create the assembly.
Regex.CompileToAssembly(rciList, an)
End Sub
End Module
正则表达式模式 (\w)\1+
的定义如下表所示。
模式 | 说明 |
---|---|
(\w) |
匹配任意单词字符,并将其分配给第一个捕获组。 |
\1+ |
匹配第一个捕获组的值的一个或多个匹配项。 |
以下示例使用 DuplicatedChars
正则表达式来标识字符串数组中的重复字符。 调用 DuplicatedChars
构造函数时,它会将超时间隔更改为 0.5 秒。
using CustomRegexes;
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
var rgx = new DuplicateChars(TimeSpan.FromSeconds(.5));
string[] values = { "Greeeeeat", "seed", "deed", "beam",
"loop", "Aardvark" };
// Display regex information.
Console.WriteLine("Regular Expression Pattern: {0}", rgx);
Console.WriteLine("Regex timeout value: {0} seconds\n",
rgx.MatchTimeout.TotalSeconds);
// Display matching information.
foreach (var value in values) {
Match m = rgx.Match(value);
if (m.Success)
Console.WriteLine("'{0}' found in '{1}' at positions {2}-{3}",
m.Value, value, m.Index, m.Index + m.Length - 1);
else
Console.WriteLine("No match found in '{0}'", value);
}
}
}
// The example displays the following output:
// Regular Expression Pattern: (\w)\1+
// Regex timeout value: 0.5 seconds
//
// //eeeee// found in //Greeeeeat// at positions 2-6
// //ee// found in //seed// at positions 1-2
// //ee// found in //deed// at positions 1-2
// No match found in //beam//
// //oo// found in //loop// at positions 1-2
// //Aa// found in //Aardvark// at positions 0-1
Imports CustomRegexes
Imports System.Text.RegularExpressions
Module Example
Public Sub Main()
Dim rgx As New DuplicateChars(TimeSpan.FromSeconds(.5))
Dim values() As String = { "Greeeeeat", "seed", "deed", "beam",
"loop", "Aardvark" }
' Display regex information.
Console.WriteLine("Regular Expression Pattern: {0}", rgx)
Console.WriteLine("Regex timeout value: {0} seconds",
rgx.MatchTimeout.TotalSeconds)
Console.WriteLine()
' Display matching information.
For Each value In values
Dim m As Match = rgx.Match(value)
If m.Success Then
Console.WriteLine("'{0}' found in '{1}' at positions {2}-{3}",
m.Value, value, m.Index, m.Index + m.Length - 1)
Else
Console.WriteLine("No match found in '{0}'", value)
End If
Next
End Sub
End Module
' The example displays the following output:
' Regular Expression Pattern: (\w)\1+
' Regex timeout value: 0.5 seconds
'
' 'eeeee' found in 'Greeeeeat' at positions 2-6
' 'ee' found in 'seed' at positions 1-2
' 'ee' found in 'deed' at positions 1-2
' No match found in 'beam'
' 'oo' found in 'loop' at positions 1-2
' 'Aa' found in 'Aardvark' at positions 0-1
注解
构造函数的每个参数 RegexCompilationInfo(String, RegexOptions, String, String, Boolean) 直接对应于 类的 RegexCompilationInfo 一个属性。 由于所有属性都是可读/写的,因此也可以直接分配其值。
方法 CompileToAssembly 生成包含已编译正则表达式的程序集。 因此,不应将 指定 Compiled 为 的值之 options
一。
如果 ispublic
为 true
,则为编译的正则表达式类提供公共可访问性。 也就是说,可以从在任何程序集中执行的代码实例化它。 如果 ispublic
为 false
,则以 C#Friend
) 或 Visual Basic) 辅助功能 ( (提供internal
编译的正则表达式类。 也就是说,它只能从与正则表达式类在同一程序集中执行的代码实例化。
参数 matchTimeout
定义已编译正则表达式的默认超时间隔。 此值表示编译的正则表达式对象在操作超时和正则表达式引擎在其下一次RegexMatchTimeoutException计时检查引发异常之前执行单个匹配操作的大致时间量。 有关超时值的其他信息,请参阅 MatchTimeout 属性。
重要
建议始终为已编译的正则表达式设置默认超时值。 正则表达式库的使用者可以通过将表示新超时间隔的值传递给 TimeSpan 此构造函数重载来替代该超时值。