using System.Text.RegularExpressions;
namespace CoreAgent.Domain.Helpers;
///
/// Linux WebSocket响应日志解析器
///
public static class LinuxWsResponseParser
{
private static readonly Regex _regexWs = new Regex(@"\{(?:[^{}]*|\{(?:[^{}]*|\{[^{}]*\})*\})*\}");
///
/// 解析Linux WebSocket响应日志
///
/// 输入日志字符串
/// 解析后的日志内容
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; // 没有找到匹配的'}'
}
}