aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJP Dillingham <jpdillingham@gmail.com>2021-08-24 08:14:03 -0500
committerJP Dillingham <jpdillingham@gmail.com>2021-08-24 08:14:03 -0500
commit0b99e719bee99699376fe7e02bea2c92f3321034 (patch)
tree509ba2ad611181a7c0d3d41f7060513a769ac993 /src
parentdbf92255737057a7603366684456f90166a88045 (diff)
downloadslskd-0b99e719bee99699376fe7e02bea2c92f3321034.tar.gz
add IsValidRegex
Diffstat (limited to 'src')
-rw-r--r--src/slskd/Common/Extensions.cs25
1 files changed, 25 insertions, 0 deletions
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;
/// <summary>
/// Extensions.
@@ -118,6 +119,30 @@ namespace slskd
}
/// <summary>
+ /// Determines whether the string is a valid regular expression.
+ /// </summary>
+ /// <param name="pattern">The string to validate.</param>
+ /// <returns>A value indicating whether the string is a valid regular expression.</returns>
+ 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;
+ }
+
+ /// <summary>
/// Deeply compares this object with the specified object and returns a list of properties that are different.
/// </summary>
/// <param name="left">The left side of the comparison.</param>