Regex.IsMatch 是.NET中用于判断正则表达式是否在输入字符串中找到匹配项的静态方法,属于 System.Text.RegularExpressions 命名空间。
核心功能
匹配验证:直接返回布尔值(true/false),无需检索匹配内容,适合快速校验字符串是否符合特定模式 。
超时控制:支持设置超时时间(TimeSpan),防止因复杂正则表达式导致程序卡死 。
常见重载形式
IsMatch(string input):使用当前 [Regex]实例的模式匹配输入字符串 。
IsMatch(string input, string pattern):指定正则表达式模式进行匹配 。
IsMatch(string input, string pattern, RegexOptions options):添加匹配选项(如忽略大小写) 。
IsMatch(string input, string pattern, RegexOptions options, TimeSpan matchTimeout):设置超时时间 。
使用场景
快速校验:如验证邮箱格式、电话号码等 。
安全性控制:通过超时避免恶意输入导致的性能问题 。