Files
ai-security-xdr/haobang-security-xdr/syslog-consumer/src/main/java/com/common/util/RegexTextParser.java
2026-01-11 15:33:22 +08:00

204 lines
6.8 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package com.common.util;
import org.springframework.stereotype.Component;
import java.util.LinkedHashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 正则表达式文本解析工具类
*/
@Component
public class RegexTextParser {
/**
* 使用正则表达式解析文本数据
* @param text 原始文本
* @param regex 正则表达式(必须包含捕获组)
* @return LinkedHashMap对象key为序号字符串value为实际值
*/
public static LinkedHashMap<String, Object> parseWithRegex(String text, String regex) {
LinkedHashMap<String, Object> resultMap = new LinkedHashMap<>();
if (text == null || text.trim().isEmpty()) {
return resultMap;
}
if (regex == null || regex.isEmpty()) {
throw new IllegalArgumentException("正则表达式不能为空");
}
try {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
// 获取所有捕获组从group(1)开始group(0)是整个匹配)
int groupCount = matcher.groupCount();
if (groupCount == 0) {
// 如果没有捕获组,将整个匹配作为第一个元素
resultMap.put("1", matcher.group(0));
} else {
// 遍历所有捕获组
for (int i = 1; i <= groupCount; i++) {
String value = matcher.group(i);
resultMap.put(String.valueOf(i), value != null ? value.trim() : "");
}
}
}
return resultMap;
} catch (Exception e) {
throw new RuntimeException("正则表达式解析失败: " + e.getMessage(), e);
}
}
/**
* 使用正则表达式解析文本数据(支持多次匹配)
* @param text 原始文本
* @param regex 正则表达式
* @param matchAll 是否匹配所有结果
* @return LinkedHashMap对象key为匹配序号value为实际值
*/
public static LinkedHashMap<String, Object> parseWithRegex(String text, String regex, boolean matchAll) {
LinkedHashMap<String, Object> resultMap = new LinkedHashMap<>();
if (text == null || text.trim().isEmpty()) {
return resultMap;
}
if (regex == null || regex.isEmpty()) {
throw new IllegalArgumentException("正则表达式不能为空");
}
if (!matchAll) {
return parseWithRegex(text, regex);
}
try {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(text);
int matchCount = 0;
while (matcher.find()) {
matchCount++;
int groupCount = matcher.groupCount();
if (groupCount == 0) {
// 如果没有捕获组,将整个匹配作为一个元素
resultMap.put(String.valueOf(matchCount), matcher.group(0));
} else {
// 对于每个匹配,将捕获组按顺序存储
for (int i = 1; i <= groupCount; i++) {
String key = matchCount + "_" + i; // 格式匹配序号_组号
String value = matcher.group(i);
resultMap.put(key, value != null ? value.trim() : "");
}
}
}
return resultMap;
} catch (Exception e) {
throw new RuntimeException("正则表达式解析失败: " + e.getMessage(), e);
}
}
/**
* 使用正则表达式解析文本数据(带类型转换)
* @param text 原始文本
* @param regex 正则表达式
* @param valueType 值类型
* @return LinkedHashMap对象
*/
public static LinkedHashMap<String, Object> parseWithRegex(String text, String regex, ValueType valueType) {
LinkedHashMap<String, Object> resultMap = parseWithRegex(text, regex);
// 应用类型转换
resultMap.replaceAll((key, value) -> convertValue(value.toString(), valueType));
return resultMap;
}
/**
* 使用正则表达式分割文本类似String.split但返回LinkedHashMap
* @param text 原始文本
* @param regex 分割正则表达式
* @return LinkedHashMap对象
*/
public static LinkedHashMap<String, Object> splitWithRegex(String text, String regex) {
LinkedHashMap<String, Object> resultMap = new LinkedHashMap<>();
if (text == null || text.trim().isEmpty()) {
return resultMap;
}
if (regex == null || regex.isEmpty()) {
throw new IllegalArgumentException("分割正则表达式不能为空");
}
try {
String[] parts = text.split(regex, -1); // 使用-1保留空字符串
for (int i = 0; i < parts.length; i++) {
resultMap.put(String.valueOf(i + 1), parts[i].trim());
}
return resultMap;
} catch (Exception e) {
throw new RuntimeException("正则表达式分割失败: " + e.getMessage(), e);
}
}
/**
* 提取引号内的内容(专用方法,处理示例中的情况)
* @param text 原始文本
* @return LinkedHashMap对象
*/
public static LinkedHashMap<String, Object> extractQuotedContent(String text) {
// 正则表达式匹配双引号内的内容,忽略转义引号
String regex = "\"([^\"]*)\"";
return parseWithRegex(text, regex, true);
}
/**
* 值类型枚举
*/
public enum ValueType {
STRING, INTEGER, LONG, DOUBLE, BOOLEAN, DATE
}
/**
* 值类型转换
*/
private static Object convertValue(String value, ValueType valueType) {
if (value == null || value.isEmpty()) {
return null;
}
try {
switch (valueType) {
case INTEGER:
return Integer.parseInt(value);
case LONG:
return Long.parseLong(value);
case DOUBLE:
return Double.parseDouble(value);
case BOOLEAN:
return Boolean.parseBoolean(value) || "1".equals(value) || "true".equalsIgnoreCase(value);
case DATE:
// 简单日期解析实际项目中可以使用DateTimeFormatter
return java.sql.Timestamp.valueOf(value);
case STRING:
default:
return value;
}
} catch (Exception e) {
// 转换失败时返回原始字符串
return value;
}
}
}