From 094df723ccce940eea0c9c981bc2ab3b4c99fe94 Mon Sep 17 00:00:00 2001 From: pengjia <2550420029@qq.com> Date: Wed, 21 Jan 2026 11:24:00 +0800 Subject: [PATCH 1/3] [hertzbeat-common]feature: Extracting groups from strings using regular expressions --- .../common/util/JexlCommonFunction.java | 27 +++++++++++++++++-- .../hertzbeat/common/util/JexlTest.java | 13 +++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/JexlCommonFunction.java b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/JexlCommonFunction.java index bc4b1bcf2f3..d5ac52f187e 100644 --- a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/JexlCommonFunction.java +++ b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/JexlCommonFunction.java @@ -19,11 +19,12 @@ package org.apache.hertzbeat.common.util; +import java.util.regex.Matcher; import java.util.regex.Pattern; /** * the common function for jexl str equals, match, contains, etc. - * sys:now() + * sys:now() */ public class JexlCommonFunction { @@ -94,5 +95,27 @@ public boolean matches(String str, String regex) { } return Pattern.compile(regex).matcher(str).matches(); } - + + /** + * Extracting groups from strings using regular expressions + * @param str Input string + * @param regex regular expression pattern + * @param groupIndex The group index to be extracted (0 for the entire match, 1 for the first group) + * @ return The extracted string does not match and returns null + */ + public String group(String str, String regex, int groupIndex) { + if (str == null || regex == null) { + return null; + } + try { + Matcher matcher = Pattern.compile(regex).matcher(str); + if (matcher.find() && groupIndex <= matcher.groupCount()) { + return matcher.group(groupIndex); + } + } catch (Exception e) { + // 正则表达式错误时返回null + } + return null; + } + } diff --git a/hertzbeat-common/src/test/java/org/apache/hertzbeat/common/util/JexlTest.java b/hertzbeat-common/src/test/java/org/apache/hertzbeat/common/util/JexlTest.java index fa502f8f6b4..9cec79e44e3 100644 --- a/hertzbeat-common/src/test/java/org/apache/hertzbeat/common/util/JexlTest.java +++ b/hertzbeat-common/src/test/java/org/apache/hertzbeat/common/util/JexlTest.java @@ -501,6 +501,19 @@ void testMatchesFunctionWithRegexNotMatch() { Assertions.assertFalse((Boolean) o); } + @Test + void testGroupFunctionWithRegexWithGroupIndex() { + Map functions = Maps.newLinkedHashMap(); + functions.put("sys", new JexlCommonFunction()); + jexlBuilder.namespaces(functions); + JexlEngine jexl = jexlBuilder.create(); + JexlContext context = new MapContext(); + context.set("x", "akka.tcp://flink@hdp-hadoop3:45534/user/rpc/taskmanager_0"); + JexlExpression e = jexl.createExpression("sys:group(x, '@([^:]+):',1)"); + Object o = e.evaluate(context); + Assertions.assertEquals("hdp-hadoop3", o); + } + @Test void testMatchesFunctionWithRegexAndSpace() { Map functions = Maps.newLinkedHashMap(); From 6b6f9ae0d6357641e567bca09bb5f6a1b466288a Mon Sep 17 00:00:00 2001 From: pengjia <2550420029@qq.com> Date: Wed, 21 Jan 2026 11:51:27 +0800 Subject: [PATCH 2/3] [hertzbeat-common]feature: Extracting groups from strings using regular expressions --- .../org/apache/hertzbeat/common/util/JexlCommonFunction.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/JexlCommonFunction.java b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/JexlCommonFunction.java index d5ac52f187e..18ec5085e4e 100644 --- a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/JexlCommonFunction.java +++ b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/JexlCommonFunction.java @@ -113,7 +113,7 @@ public String group(String str, String regex, int groupIndex) { return matcher.group(groupIndex); } } catch (Exception e) { - // 正则表达式错误时返回null + // Return null when regular expression is incorrect } return null; } From e4a70c13c681d649d8e57824c8f82e477287eedc Mon Sep 17 00:00:00 2001 From: pengjia <2550420029@qq.com> Date: Wed, 21 Jan 2026 11:24:00 +0800 Subject: [PATCH 3/3] [hertzbeat-common]feature: Extracting groups from strings using regular expressions --- .../common/util/JexlCommonFunction.java | 27 +++++++++++++++++-- .../hertzbeat/common/util/JexlTest.java | 13 +++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/JexlCommonFunction.java b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/JexlCommonFunction.java index bc4b1bcf2f3..18ec5085e4e 100644 --- a/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/JexlCommonFunction.java +++ b/hertzbeat-common/src/main/java/org/apache/hertzbeat/common/util/JexlCommonFunction.java @@ -19,11 +19,12 @@ package org.apache.hertzbeat.common.util; +import java.util.regex.Matcher; import java.util.regex.Pattern; /** * the common function for jexl str equals, match, contains, etc. - * sys:now() + * sys:now() */ public class JexlCommonFunction { @@ -94,5 +95,27 @@ public boolean matches(String str, String regex) { } return Pattern.compile(regex).matcher(str).matches(); } - + + /** + * Extracting groups from strings using regular expressions + * @param str Input string + * @param regex regular expression pattern + * @param groupIndex The group index to be extracted (0 for the entire match, 1 for the first group) + * @ return The extracted string does not match and returns null + */ + public String group(String str, String regex, int groupIndex) { + if (str == null || regex == null) { + return null; + } + try { + Matcher matcher = Pattern.compile(regex).matcher(str); + if (matcher.find() && groupIndex <= matcher.groupCount()) { + return matcher.group(groupIndex); + } + } catch (Exception e) { + // Return null when regular expression is incorrect + } + return null; + } + } diff --git a/hertzbeat-common/src/test/java/org/apache/hertzbeat/common/util/JexlTest.java b/hertzbeat-common/src/test/java/org/apache/hertzbeat/common/util/JexlTest.java index fa502f8f6b4..9cec79e44e3 100644 --- a/hertzbeat-common/src/test/java/org/apache/hertzbeat/common/util/JexlTest.java +++ b/hertzbeat-common/src/test/java/org/apache/hertzbeat/common/util/JexlTest.java @@ -501,6 +501,19 @@ void testMatchesFunctionWithRegexNotMatch() { Assertions.assertFalse((Boolean) o); } + @Test + void testGroupFunctionWithRegexWithGroupIndex() { + Map functions = Maps.newLinkedHashMap(); + functions.put("sys", new JexlCommonFunction()); + jexlBuilder.namespaces(functions); + JexlEngine jexl = jexlBuilder.create(); + JexlContext context = new MapContext(); + context.set("x", "akka.tcp://flink@hdp-hadoop3:45534/user/rpc/taskmanager_0"); + JexlExpression e = jexl.createExpression("sys:group(x, '@([^:]+):',1)"); + Object o = e.evaluate(context); + Assertions.assertEquals("hdp-hadoop3", o); + } + @Test void testMatchesFunctionWithRegexAndSpace() { Map functions = Maps.newLinkedHashMap();