Regex.IsMatch 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
指示正则表达式是否在输入字符串中找到匹配项。
重载
| 名称 | 说明 |
|---|---|
| IsMatch(String, String, RegexOptions, TimeSpan) |
指示指定的正则表达式是否使用指定的匹配选项和超时间隔在指定的输入字符串中找到匹配项。 |
| IsMatch(ReadOnlySpan<Char>, String, RegexOptions, TimeSpan) |
指示指定的正则表达式是否使用指定的匹配选项和超时间隔在指定的输入范围中找到匹配项。 |
| IsMatch(String, String, RegexOptions) |
指示指定的正则表达式是否使用指定的匹配选项在指定的输入字符串中找到匹配项。 |
| IsMatch(ReadOnlySpan<Char>, String, RegexOptions) |
指示指定的正则表达式是否使用指定的匹配选项在指定的输入范围中找到匹配项。 |
| IsMatch(String, String) |
指示指定的正则表达式是否在指定的输入字符串中找到匹配项。 |
| IsMatch(ReadOnlySpan<Char>, Int32) |
指示构造函数中指定的 Regex 正则表达式是否在指定的输入范围中找到匹配项,从指定位置开始。 |
| IsMatch(ReadOnlySpan<Char>, String) |
指示指定的正则表达式是否在指定的输入范围中找到匹配项。 |
| IsMatch(String, Int32) |
指示构造函数中指定的 Regex 正则表达式是否在指定的输入字符串中找到匹配项,从字符串中的指定起始位置开始。 |
| IsMatch(String) |
指示构造函数中指定的 Regex 正则表达式是否在指定的输入字符串中找到匹配项。 |
| IsMatch(ReadOnlySpan<Char>) |
指示构造函数中指定的 Regex 正则表达式是否在指定的输入范围中找到匹配项。 |
IsMatch(String, String, RegexOptions, TimeSpan)
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
指示指定的正则表达式是否使用指定的匹配选项和超时间隔在指定的输入字符串中找到匹配项。
public:
static bool IsMatch(System::String ^ input, System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options, TimeSpan matchTimeout);
public static bool IsMatch(string input, string pattern, System.Text.RegularExpressions.RegexOptions options, TimeSpan matchTimeout);
static member IsMatch : string * string * System.Text.RegularExpressions.RegexOptions * TimeSpan -> bool
Public Shared Function IsMatch (input As String, pattern As String, options As RegexOptions, matchTimeout As TimeSpan) As Boolean
参数
- input
- String
要搜索匹配项的字符串。
- pattern
- String
要匹配的正则表达式模式。
- options
- RegexOptions
提供匹配选项的枚举值的按位组合。
- matchTimeout
- TimeSpan
超时间隔,或 InfiniteMatchTimeout 指示方法不应超时。
返回
true 如果正则表达式找到匹配项,否则,为 false.
例外
发生正则表达式分析错误。
input 或 pattern 为 null.
options 不是有效 RegexOptions 值。
- 或 - matchTimeout 为负、零或大于大约 24 天。
注解
该方法 IsMatch(String, String, RegexOptions, TimeSpan) 通常用于验证字符串或确保字符串符合特定模式,而无需检索该字符串以供后续操作。 若要检索匹配的字符串,请改为调用 Match(String, String, RegexOptions, TimeSpan) 或 Matches(String, String, RegexOptions, TimeSpan) 方法。
静态 IsMatch(String, String, RegexOptions, TimeSpan) 方法等效于使用指定的模式和选项构造 Regex 对象并调用 IsMatch(String) 实例方法。 模式缓存供正则表达式引擎快速检索。
该 matchTimeout 参数指定模式匹配方法在超时之前应尝试查找匹配的时间。设置超时间隔可防止依赖过度回溯的正则表达式在处理包含接近匹配项的输入时停止响应。 如果未在该时间间隔中找到匹配项,该方法将 RegexMatchTimeoutException 引发异常。
matchTimeout 重写为方法在其中执行的应用程序域定义的任何默认超时值。
调用方说明
建议将 matchTimeout 参数设置为适当的值,例如两秒。 如果通过指定 InfiniteMatchTimeout禁用超时,则正则表达式引擎的性能略有提高。 但是,应仅在以下情况下禁用超时:
当正则表达式处理的输入派生自已知且受信任的源或包含静态文本时。 这不包括用户动态输入的文本。
当正则表达式模式经过全面测试以确保它有效地处理匹配项、非匹配项和接近匹配项时。
当正则表达式模式不包含任何已知在处理接近匹配时导致过度回溯的语言元素。
另请参阅
适用于
IsMatch(ReadOnlySpan<Char>, String, RegexOptions, TimeSpan)
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
指示指定的正则表达式是否使用指定的匹配选项和超时间隔在指定的输入范围中找到匹配项。
public:
static bool IsMatch(ReadOnlySpan<char> input, System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options, TimeSpan matchTimeout);
public static bool IsMatch(ReadOnlySpan<char> input, string pattern, System.Text.RegularExpressions.RegexOptions options, TimeSpan matchTimeout);
static member IsMatch : ReadOnlySpan<char> * string * System.Text.RegularExpressions.RegexOptions * TimeSpan -> bool
Public Shared Function IsMatch (input As ReadOnlySpan(Of Char), pattern As String, options As RegexOptions, matchTimeout As TimeSpan) As Boolean
参数
- input
- ReadOnlySpan<Char>
要搜索匹配项的跨度。
- pattern
- String
要匹配的正则表达式模式。
- options
- RegexOptions
提供匹配选项的枚举值的按位组合。
- matchTimeout
- TimeSpan
超时间隔,或 InfiniteMatchTimeout 指示方法不应超时。
返回
true 如果正则表达式找到匹配项,否则,为 false.
例外
发生正则表达式分析错误。
pattern 是 null。
options 不是有效 RegexOptions 值 - 或 - matchTimeout 为负、零或大于大约 24 天。
适用于
IsMatch(String, String, RegexOptions)
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
指示指定的正则表达式是否使用指定的匹配选项在指定的输入字符串中找到匹配项。
public:
static bool IsMatch(System::String ^ input, System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options);
public static bool IsMatch(string input, string pattern, System.Text.RegularExpressions.RegexOptions options);
static member IsMatch : string * string * System.Text.RegularExpressions.RegexOptions -> bool
Public Shared Function IsMatch (input As String, pattern As String, options As RegexOptions) As Boolean
参数
- input
- String
要搜索匹配项的字符串。
- pattern
- String
要匹配的正则表达式模式。
- options
- RegexOptions
提供匹配选项的枚举值的按位组合。
返回
true 如果正则表达式找到匹配项,否则,为 false.
例外
发生正则表达式分析错误。
input 或 pattern 为 null.
options 不是有效 RegexOptions 值。
注解
该方法 IsMatch(String, String, RegexOptions) 通常用于验证字符串或确保字符串符合特定模式,而无需检索该字符串以供后续操作。 若要检索匹配的字符串,请改为调用 Match(String, String, RegexOptions) 或 Matches(String, String, RegexOptions) 方法。
静态 IsMatch(String, String, RegexOptions) 方法等效于使用指定的模式和选项构造 Regex 对象并调用 IsMatch(String) 实例方法。 模式缓存供正则表达式引擎快速检索。
调用方说明
此方法在一个时间间隔后超时,该值等于调用它的应用程序域的默认超时值。 如果未为应用程序域定义超时值,则使用阻止方法超时的值 InfiniteMatchTimeout。 用于验证模式匹配的建议静态方法是 IsMatch(String, String, RegexOptions, TimeSpan),这允许你设置超时间隔。
另请参阅
适用于
IsMatch(ReadOnlySpan<Char>, String, RegexOptions)
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
指示指定的正则表达式是否使用指定的匹配选项在指定的输入范围中找到匹配项。
public:
static bool IsMatch(ReadOnlySpan<char> input, System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options);
public static bool IsMatch(ReadOnlySpan<char> input, string pattern, System.Text.RegularExpressions.RegexOptions options);
static member IsMatch : ReadOnlySpan<char> * string * System.Text.RegularExpressions.RegexOptions -> bool
Public Shared Function IsMatch (input As ReadOnlySpan(Of Char), pattern As String, options As RegexOptions) As Boolean
参数
- input
- ReadOnlySpan<Char>
要搜索匹配项的跨度。
- pattern
- String
要匹配的正则表达式模式。
- options
- RegexOptions
提供匹配选项的枚举值的按位组合。
返回
true 如果正则表达式找到匹配项,否则,为 false.
例外
发生正则表达式分析错误。
pattern 是 null。
options 不是有效 RegexOptions 值。
适用于
IsMatch(String, String)
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
指示指定的正则表达式是否在指定的输入字符串中找到匹配项。
public:
static bool IsMatch(System::String ^ input, System::String ^ pattern);
public static bool IsMatch(string input, string pattern);
static member IsMatch : string * string -> bool
Public Shared Function IsMatch (input As String, pattern As String) As Boolean
参数
- input
- String
要搜索匹配项的字符串。
- pattern
- String
要匹配的正则表达式模式。
返回
true 如果正则表达式找到匹配项,否则,为 false.
例外
发生正则表达式分析错误。
input 或 pattern 为 null.
注解
该方法 IsMatch(String, String) 通常用于验证字符串或确保字符串符合特定模式,而无需检索该字符串以供后续操作。 若要检索匹配的字符串,请改为调用 Match(String, String) 或 Matches(String, String) 方法。
静态 IsMatch(String, String) 方法等效于使用指定的模式构造 Regex 对象并调用 IsMatch(String) 实例方法。 模式缓存供正则表达式引擎快速检索。
调用方说明
此方法在一个等于调用该方法的应用程序域的默认超时值的间隔后超时。 如果未为应用程序域定义超时值,则使用阻止方法超时的值 InfiniteMatchTimeout。 用于验证模式匹配的建议静态方法是 IsMatch(String, String, RegexOptions, TimeSpan),这允许你设置超时间隔。
另请参阅
适用于
IsMatch(ReadOnlySpan<Char>, Int32)
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
指示构造函数中指定的 Regex 正则表达式是否在指定的输入范围中找到匹配项,从指定位置开始。
public:
bool IsMatch(ReadOnlySpan<char> input, int startat);
public bool IsMatch(ReadOnlySpan<char> input, int startat);
member this.IsMatch : ReadOnlySpan<char> * int -> bool
Public Function IsMatch (input As ReadOnlySpan(Of Char), startat As Integer) As Boolean
参数
- input
- ReadOnlySpan<Char>
要搜索匹配项的跨度。
- startat
- Int32
从零开始搜索的字符位置。
返回
true 如果正则表达式找到匹配项,否则,为 false.
例外
适用于
IsMatch(ReadOnlySpan<Char>, String)
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
指示指定的正则表达式是否在指定的输入范围中找到匹配项。
public:
static bool IsMatch(ReadOnlySpan<char> input, System::String ^ pattern);
public static bool IsMatch(ReadOnlySpan<char> input, string pattern);
static member IsMatch : ReadOnlySpan<char> * string -> bool
Public Shared Function IsMatch (input As ReadOnlySpan(Of Char), pattern As String) As Boolean
参数
- input
- ReadOnlySpan<Char>
要搜索匹配项的跨度。
- pattern
- String
要匹配的正则表达式模式。
返回
true 如果正则表达式找到匹配项,否则,为 false.
例外
发生正则表达式分析错误。
pattern 是 null。
适用于
IsMatch(String, Int32)
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
指示构造函数中指定的 Regex 正则表达式是否在指定的输入字符串中找到匹配项,从字符串中的指定起始位置开始。
public:
bool IsMatch(System::String ^ input, int startat);
public bool IsMatch(string input, int startat);
member this.IsMatch : string * int -> bool
Public Function IsMatch (input As String, startat As Integer) As Boolean
参数
- input
- String
要搜索匹配项的字符串。
- startat
- Int32
开始搜索的字符位置。
返回
true 如果正则表达式找到匹配项,否则,为 false.
例外
input 是 null。
startat 小于零或大于长度 input。
注解
该方法 IsMatch(String, Int32) 通常用于验证字符串或确保字符串符合特定模式,而无需检索该字符串以供后续操作。 若要检索匹配的字符串,请改为调用 Match(String, Int32) 或 Matches(String, Int32) 方法。
有关更多详细信息 startat,请参阅其备注 Match(String, Int32)。
另请参阅
适用于
IsMatch(String)
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
指示构造函数中指定的 Regex 正则表达式是否在指定的输入字符串中找到匹配项。
public:
bool IsMatch(System::String ^ input);
public bool IsMatch(string input);
member this.IsMatch : string -> bool
Public Function IsMatch (input As String) As Boolean
参数
- input
- String
要搜索匹配项的字符串。
返回
true 如果正则表达式找到匹配项,否则,为 false.
例外
input 是 null。
注解
该方法 IsMatch(String) 通常用于验证字符串或确保字符串符合特定模式,而无需检索该字符串以供后续操作。 若要确定一个或多个字符串是否与正则表达式模式匹配,并检索它们以供后续操作,请调用 Match(String) 或 Matches(String) 方法。
如果匹配操作的执行时间超过 RegexMatchTimeoutException 构造函数指定的超时间隔,则会引发 Regex(String, RegexOptions, TimeSpan) 异常。 如果在调用构造函数时未设置超时间隔,当在创建 Regex 对象的应用程序域中操作超过任何已建立的超时值时,将会引发异常。 如果在构造函数调用或应用程序域的属性中未定义 Regex 超时,或者超时值为 InfiniteMatchTimeout,则不会引发异常。
另请参阅
适用于
IsMatch(ReadOnlySpan<Char>)
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
指示构造函数中指定的 Regex 正则表达式是否在指定的输入范围中找到匹配项。
public:
bool IsMatch(ReadOnlySpan<char> input);
public bool IsMatch(ReadOnlySpan<char> input);
member this.IsMatch : ReadOnlySpan<char> -> bool
Public Function IsMatch (input As ReadOnlySpan(Of Char)) As Boolean
参数
- input
- ReadOnlySpan<Char>
要搜索匹配项的跨度。
返回
true 如果正则表达式找到匹配项,否则,为 false.