From 0fec2377a3e9bf582610ad346f6ad20c6d622688 Mon Sep 17 00:00:00 2001 From: ForJustice Date: Wed, 22 Jan 2025 13:32:12 +0000 Subject: [PATCH] =?UTF-8?q?!108=20=E6=96=B0=E5=A2=9E=E6=AD=A3=E5=88=99?= =?UTF-8?q?=E8=A1=A8=E8=BE=BE=E5=BC=8F=20match=20=E5=87=BD=E6=95=B0?= =?UTF-8?q?=EF=BC=8C=E8=BF=94=E5=9B=9E=E5=8C=B9=E9=85=8D=E7=9A=84=E6=95=B0?= =?UTF-8?q?=E7=BB=84=20*=20=E6=96=B0=E5=A2=9E=E6=AD=A3=E5=88=99=E8=A1=A8?= =?UTF-8?q?=E8=BE=BE=E5=BC=8F=20match=20=E5=87=BD=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=8C=B9=E9=85=8D=E7=9A=84=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/nop/commons/text/regex/IRegex.java | 2 ++ .../nop/commons/text/regex/impl/JdkRegex.java | 15 ++++++++++++- .../java/io/nop/commons/text/TestRegex.java | 21 +++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/nop-commons/src/main/java/io/nop/commons/text/regex/IRegex.java b/nop-commons/src/main/java/io/nop/commons/text/regex/IRegex.java index 01677e1ae..f70d6e419 100644 --- a/nop-commons/src/main/java/io/nop/commons/text/regex/IRegex.java +++ b/nop-commons/src/main/java/io/nop/commons/text/regex/IRegex.java @@ -17,4 +17,6 @@ default boolean find(String text){ } List exec(String text); + + List match(String text); } diff --git a/nop-commons/src/main/java/io/nop/commons/text/regex/impl/JdkRegex.java b/nop-commons/src/main/java/io/nop/commons/text/regex/impl/JdkRegex.java index 4c9fe29a2..d12bc0a45 100644 --- a/nop-commons/src/main/java/io/nop/commons/text/regex/impl/JdkRegex.java +++ b/nop-commons/src/main/java/io/nop/commons/text/regex/impl/JdkRegex.java @@ -45,4 +45,17 @@ public List exec(String text) { return null; } } -} \ No newline at end of file + + @Override + public List match(String text) { + if (StringHelper.isEmpty(text)) + return null; + + Matcher matcher = pattern.matcher(text); + List ret = new ArrayList<>(); + while (matcher.find()) { + ret.add(matcher.group()); + } + return ret.isEmpty() ? null : ret; + } +} diff --git a/nop-commons/src/test/java/io/nop/commons/text/TestRegex.java b/nop-commons/src/test/java/io/nop/commons/text/TestRegex.java index 8abd3e546..39228fbae 100644 --- a/nop-commons/src/test/java/io/nop/commons/text/TestRegex.java +++ b/nop-commons/src/test/java/io/nop/commons/text/TestRegex.java @@ -17,6 +17,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertNull; public class TestRegex { @Test @@ -69,4 +70,24 @@ public void checkIRegex() { String simpleName = list.get(1); assertEquals("SimpleDomainServiceDto", simpleName); } + + @Test + public void checkIRegexMatch() { + String input = "xxxxAA10002 BB10022 abcdefAA10003saasdsd"; + // 正则表达式, 提取匹配的字符串 + String regexString = "(AA\\d{4,}|BB\\d{4,})"; + IRegex regex = RegexHelper.compileRegex(regexString); + + List list = regex.match(input); + assertEquals(3, list.size()); + assertEquals("AA10002", list.get(0)); + assertEquals("BB10022", list.get(1)); + assertEquals("AA10003", list.get(2)); + + // 正则表达式, 不匹配应该返回 null + String regexString2 = "(CC.*)"; + IRegex regex2 = RegexHelper.compileRegex(regexString2); + List list2 = regex2.match(input); + assertNull(list2); + } }