using System.Text.RegularExpressions; namespace YandexMusic.API.Extensions { public static class StringExtensions { public static string ReplaceRegex(this string str, string regExpr, string replStr, RegexOptions options = RegexOptions.IgnoreCase) { return str == null ? string.Empty : Regex.Replace(str, regExpr, replStr); } public static string SplitByCapitalLetter(this string str, string delimiter) { return string.Join(delimiter, Regex.Matches(str, @"([A-Z]+)(?=([A-Z][a-z]|$)) | [A-Z][a-z].+?(?=([A-Z]|$))", RegexOptions.IgnorePatternWhitespace) .Cast() .Select(m => m.ToString())); } /// /// Проверяет соответствие регулярному выражению /// public static bool IsMatch(this string str, string pattern, RegexOptions options) { return Regex.IsMatch(str, pattern, options); } /// /// Проверяет соответствие регулярному выражению /// public static bool IsMatch(this string str, string pattern) { return IsMatch(str, pattern, RegexOptions.IgnoreCase); } /// /// Возвращает совпадения для регулярного выражения /// public static string[] GetMatches(this string str, string pattern, RegexOptions options = RegexOptions.IgnoreCase) { return str.IsMatch(pattern, options) ? Regex.Matches(str, pattern, options) .Cast() .Select(m => m.Value) .ToArray() : new string[] { }; } } }