更新:2007 年 11 月
用修改模式的选项为指定的正则表达式初始化并编译 Regex 类的一个新实例。
命名空间:
System.Text.RegularExpressions 程序集:
System(在 System.dll 中)
Public Sub New ( _
pattern As String, _
options As RegexOptions _
)
Dim pattern As String
Dim options As RegexOptions
Dim instance As New Regex(pattern, _
options)
public Regex(
string pattern,
RegexOptions options
)
public:
Regex(
String^ pattern,
RegexOptions options
)
public Regex(
String pattern,
RegexOptions options
)
public function Regex(
pattern : String,
options : RegexOptions
)
pattern 参数由通过符号描述要匹配的字符串的各种正则表达式语言元素组成。有关正则表达式的更多信息,请参见 .NET Framework 正则表达式和正则表达式语言元素主题。
Regex 对象是不可变的,这意味着它只能用于创建时定义的匹配参数。不过可以任意多次地使用它,而无需重新编译。
下面的示例阐释了如何使用此构造函数实例化一个正则表达式,该表达式与以字母“a”或“t”开头的任何单词相匹配。
Dim pattern As String = "\b[at]\w+"
Dim options As RegexOptions = RegexOptions.IgnoreCase Or RegexOptions.Compiled
Dim text As String = "The threaded application ate up the thread pool as it executed."
Dim matches As MatchCollection
Dim optionRegex As New Regex(pattern, options)
Console.WriteLine("Parsing '{0}' with options {1}:", text, options.ToString())
' Get matches of pattern in text
matches = optionRegex.Matches(text)
' Iterate matches
For ctr As Integer = 0 to matches.Count - 1
Console.WriteLine("{0}. {1}", ctr, matches(ctr).Value)
Next
string pattern = @"\b[at]\w+";
RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Compiled;
string text = "The threaded application ate up the thread pool as it executed.";
MatchCollection matches;
Regex optionRegex = new Regex(pattern, options);
Console.WriteLine("Parsing '{0}' with options {1}:", text, options.ToString());
// Get matches of pattern in text
matches = optionRegex.Matches(text);
// Iterate matches
for (int ctr = 1; ctr <= matches.Count; ctr++)
Console.WriteLine("{0}. {1}", ctr, matches[ctr-1].Value);
请注意,匹配集合包括文本开头的单词“The”,因为 options 参数已定义了不区分大小写的比较。
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360
.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。
.NET Framework
受以下版本支持:3.5、3.0、2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:3.5、2.0、1.0
XNA Framework
受以下版本支持:2.0、1.0
参考
其他资源