Regex HelperIs Match Cached 方法
使用缓存编译的正则表达式验证字符串是否匹配指定模式。
Definition
命名空间: Suresking.Utility
程序集: Suresking.Utility (在 Suresking.Utility.dll 中) 版本:1.3.22+a27276bb8ffde055e69ff608e98aafba279e1e99
如果匹配,返回 true;否则返回 false。
程序集: Suresking.Utility (在 Suresking.Utility.dll 中) 版本:1.3.22+a27276bb8ffde055e69ff608e98aafba279e1e99
C#
public static bool IsMatchCached(
string input,
string pattern,
RegexOptions options = RegexOptions.None
)参数
- input String
- 要验证的字符串。
- pattern String
- 正则表达式模式,不能为 null 或空。
- options RegexOptions (Optional)
- 正则表达式选项。
返回值
Boolean如果匹配,返回 true;否则返回 false。
备注
适用于重复使用同一模式的场景,会自动缓存编译后的实例。
若模式仅使用一次,建议用 IsMatch(String, String, RegexOptions)。
⚠️ 请勿传入动态拼接的用户输入作为 pattern,缓存无自动过期。
示例
C#
for (int i = 0; i < 1000; i++)
{
bool match = RegexHelper.IsMatchCached(inputs[i], @"\d{3}-\d{4}");
}异常
| ArgumentNullException | input 为 null。 |
| ArgumentException | pattern 为 null 或空。 |