正则
This commit is contained in:
1 parent
b26e2c77e6
commit
2d682afd2c
4 files changed
+69
-3
No files matched your search
@@ -176,6 +176,63 @@ public static class StringExtensions {
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region 正则
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 搜索字符串中所有的正则匹配项。
|
||||||
|
/// </summary>
|
||||||
|
public static IEnumerable<string> RegexSearch(this string str, string pattern, RegexOptions options = RegexOptions.None) {
|
||||||
|
try {
|
||||||
|
return Regex.Matches(str, pattern, options).Cast<Match>().Select(m => m.Value);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
Logger.Warning(ex, $"{nameof(RegexSearch)} 失败({pattern})");
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取字符串中的第一个正则匹配项,若无匹配或失败则返回 <see langword="null"/>。
|
||||||
|
/// </summary>
|
||||||
|
public static string? RegexSeek(this string str, string pattern, RegexOptions options = RegexOptions.None) {
|
||||||
|
try {
|
||||||
|
var match = Regex.Match(str, pattern, options);
|
||||||
|
return match.Success ? match.Value : null;
|
||||||
|
} catch (Exception ex) {
|
||||||
|
Logger.Warning(ex, $"{nameof(RegexSeek)} 失败({pattern})");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 检查字符串是否匹配某正则模式。
|
||||||
|
/// </summary>
|
||||||
|
public static bool RegexCheck(this string str, string pattern, RegexOptions options = RegexOptions.None) {
|
||||||
|
try {
|
||||||
|
return Regex.IsMatch(str, pattern, options);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
Logger.Warning(ex, $"{nameof(RegexCheck)} 失败({pattern})");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 进行正则替换。
|
||||||
|
/// 失败时会抛出异常。
|
||||||
|
/// </summary>
|
||||||
|
public static string RegexReplace(this string allContents, string searchRegex, string replaceTo, RegexOptions options = RegexOptions.None) {
|
||||||
|
return Regex.Replace(allContents, searchRegex, replaceTo, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 对每个正则匹配分别进行替换。
|
||||||
|
/// 失败时会抛出异常。
|
||||||
|
/// </summary>
|
||||||
|
public static string RegexReplace(this string allContents, string searchRegex, MatchEvaluator replaceTo, RegexOptions options = RegexOptions.None) {
|
||||||
|
return Regex.Replace(allContents, searchRegex, replaceTo, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 将第一个字符转换为大写,其余字符转换为小写。
|
/// 将第一个字符转换为大写,其余字符转换为小写。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ public enum LogBehavior {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static class Logger {
|
public static class Logger {
|
||||||
public static BaseLogger Instance { get; set; } = new();
|
internal static BaseLogger Instance { get; set; } = new();
|
||||||
|
|
||||||
// 转发给实例的方法包装
|
// 转发给实例的方法包装
|
||||||
public static void Log(string message, LogLevel level = LogLevel.Info, LogBehavior behavior = LogBehavior.None, [CallerFilePath] string filePath = "")
|
public static void Log(string message, LogLevel level = LogLevel.Info, LogBehavior behavior = LogBehavior.None, [CallerFilePath] string filePath = "")
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
namespace MeloongCore;
|
||||||
|
public static class Main {
|
||||||
|
|
||||||
|
public static void Init(BaseLogger? logger = null) {
|
||||||
|
AppDomain.CurrentDomain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromSeconds(2));
|
||||||
|
if (logger != null) Logger.Instance = logger;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -88,8 +88,8 @@ public static class PathUtils {
|
|||||||
/// 去除路径末尾的文件名。
|
/// 去除路径末尾的文件名。
|
||||||
/// 若路径以分隔符结尾,则不作处理。
|
/// 若路径以分隔符结尾,则不作处理。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string? RemoveFileName(string? path) {
|
public static string RemoveFileName(string? path) {
|
||||||
if (path is null || string.IsNullOrEmpty(path)) return path;
|
if (path is null || string.IsNullOrEmpty(path)) return "";
|
||||||
if (path.EndsWithF(Path.DirectorySeparatorChar) || path.EndsWithF(Path.AltDirectorySeparatorChar)) return path;
|
if (path.EndsWithF(Path.DirectorySeparatorChar) || path.EndsWithF(Path.AltDirectorySeparatorChar)) return path;
|
||||||
return Path.GetDirectoryName(path) ?? "";
|
return Path.GetDirectoryName(path) ?? "";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user