请单击以进行评分并提供反馈
MSDN
MSDN Library
.NET 开发
.NET Framework
Regex 类
 Regex 构造函数 (String, RegexOptions)
全部折叠/全部展开 全部折叠
此页面仅适用于
Microsoft Visual Studio 2008/.NET Framework 3.5

同时提供下列产品的其他版本:
.NET Framework 类库
Regex 构造函数 (String, RegexOptions)

更新:2007 年 11 月

用修改模式的选项为指定的正则表达式初始化并编译 Regex 类的一个新实例。

命名空间:  System.Text.RegularExpressions
程序集:  System(在 System.dll 中)

Visual Basic(声明)
Public Sub New ( _
    pattern As String, _
    options As RegexOptions _
)
Visual Basic (用法)
Dim pattern As String
Dim options As RegexOptions

Dim instance As New Regex(pattern, _
    options)
C#
public Regex(
    string pattern,
    RegexOptions options
)
Visual C++
public:
Regex(
    String^ pattern, 
    RegexOptions options
)
J#
public Regex(
    String pattern,
    RegexOptions options
)
JScript
public function Regex(
    pattern : String, 
    options : RegexOptions
)

参数

pattern
类型:System..::.String

要匹配的正则表达式模式。

options
类型:System.Text.RegularExpressions..::.RegexOptions

RegexOptions 枚举值的按位“或”组合。

异常条件
ArgumentException

正则表达式语法分析错误。

ArgumentNullException

patternnullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing

ArgumentOutOfRangeException

options 包含无效标志。

pattern 参数由通过符号描述要匹配的字符串的各种正则表达式语言元素组成。有关正则表达式的更多信息,请参见 .NET Framework 正则表达式正则表达式语言元素主题。

Regex 对象是不可变的,这意味着它只能用于创建时定义的匹配参数。不过可以任意多次地使用它,而无需重新编译。

下面的示例阐释了如何使用此构造函数实例化一个正则表达式,该表达式与以字母“a”或“t”开头的任何单词相匹配。

Visual Basic
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
C#
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
社区内容   什么是社区内容?
添加新内容 RSS  批注
Processing
© 2009 Microsoft Corporation 版权所有。 保留所有权利 | 商标 | 隐私权声明
Page view tracker