3 changed files with 76 additions and 6 deletions
@ -0,0 +1,69 @@ |
|||||
|
using System.Text.RegularExpressions; |
||||
|
|
||||
|
namespace CoreAgent.Domain.Helpers; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Linux WebSocket响应日志解析器
|
||||
|
/// </summary>
|
||||
|
public static class LinuxWsResponseParser |
||||
|
{ |
||||
|
private static readonly Regex _regexWs = new Regex(@"\{(?:[^{}]*|\{(?:[^{}]*|\{[^{}]*\})*\})*\}"); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 解析Linux WebSocket响应日志
|
||||
|
/// </summary>
|
||||
|
/// <param name="input">输入日志字符串</param>
|
||||
|
/// <returns>解析后的日志内容</returns>
|
||||
|
public static string ParseWsResultLogs(this string input) |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
return GetContentBetweenOuterCurlyBraces(input); |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
string msg = ex.Message; |
||||
|
return "Parse Logs Data Is null"; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private static string GetContentBetweenOuterCurlyBraces(string input) |
||||
|
{ |
||||
|
int openBraceIndex = input.IndexOf('{'); |
||||
|
if (openBraceIndex == -1) |
||||
|
{ |
||||
|
return string.Empty; // 没有找到'{'
|
||||
|
} |
||||
|
|
||||
|
int closeBraceIndex = FindMatchingCloseBrace(input, openBraceIndex); |
||||
|
if (closeBraceIndex == -1) |
||||
|
{ |
||||
|
return string.Empty; // 没有找到匹配的'}'
|
||||
|
} |
||||
|
|
||||
|
// 提取并返回内容
|
||||
|
return input.Substring(openBraceIndex, closeBraceIndex - openBraceIndex + 1); |
||||
|
} |
||||
|
|
||||
|
private static int FindMatchingCloseBrace(string input, int openBraceIndex) |
||||
|
{ |
||||
|
int braceDepth = 1; // 从第一个'{'开始,深度为1
|
||||
|
for (int i = openBraceIndex + 1; i < input.Length; i++) |
||||
|
{ |
||||
|
if (input[i] == '{') |
||||
|
{ |
||||
|
braceDepth++; // 遇到'{',深度增加
|
||||
|
} |
||||
|
else if (input[i] == '}') |
||||
|
{ |
||||
|
braceDepth--; // 遇到'}',深度减少
|
||||
|
if (braceDepth == 0) |
||||
|
{ |
||||
|
return i; // 找到匹配的'}',返回其索引
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return -1; // 没有找到匹配的'}'
|
||||
|
} |
||||
|
} |
Loading…
Reference in new issue