From 0b99e719bee99699376fe7e02bea2c92f3321034 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 24 Aug 2021 08:14:03 -0500 Subject: add IsValidRegex --- src/slskd/Common/Extensions.cs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'src') diff --git a/src/slskd/Common/Extensions.cs b/src/slskd/Common/Extensions.cs index 20620e44..077f5b17 100644 --- a/src/slskd/Common/Extensions.cs +++ b/src/slskd/Common/Extensions.cs @@ -23,6 +23,7 @@ namespace slskd using System.Linq; using System.Reflection; using System.Text.Json; + using System.Text.RegularExpressions; /// /// Extensions. @@ -117,6 +118,30 @@ namespace slskd } } + /// + /// Determines whether the string is a valid regular expression. + /// + /// The string to validate. + /// A value indicating whether the string is a valid regular expression. + public static bool IsValidRegex(this string pattern) + { + if (string.IsNullOrWhiteSpace(pattern)) + { + return false; + } + + try + { + Regex.Match(string.Empty, pattern); + } + catch (ArgumentException) + { + return false; + } + + return true; + } + /// /// Deeply compares this object with the specified object and returns a list of properties that are different. /// -- cgit v1.2.3