From 177ab4732e9aa12215737d7390590a1dbb412e27 Mon Sep 17 00:00:00 2001 From: CoCoTea <572315466@qq.com> Date: Fri, 8 Nov 2024 14:52:08 +0800 Subject: [PATCH] =?UTF-8?q?ReUtil=E5=B7=A5=E5=85=B7=E7=B1=BB=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E2=80=9CallIndexOf=E2=80=9D=E6=96=B9=E6=B3=95?= =?UTF-8?q?=EF=BC=8C=E7=94=A8=E4=BA=8E=E6=89=BE=E5=88=B0=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=E5=8C=B9=E9=85=8D=E5=88=B0=E6=89=80=E6=9C=89?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/dromara/hutool/core/regex/ReUtil.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/hutool-core/src/main/java/org/dromara/hutool/core/regex/ReUtil.java b/hutool-core/src/main/java/org/dromara/hutool/core/regex/ReUtil.java index f68d7e891..8d5edd696 100644 --- a/hutool-core/src/main/java/org/dromara/hutool/core/regex/ReUtil.java +++ b/hutool-core/src/main/java/org/dromara/hutool/core/regex/ReUtil.java @@ -794,6 +794,46 @@ public class ReUtil { return result; } + /** + * 找到指定模式匹配到所有字符串的位置 + * + * @param regex 正则 + * @param content 字符串 + * @return 位置集合,{@code null}表示未找到 + * @since 6.0.0 + */ + public static List allIndexOf(String regex, CharSequence content) { + if (null == regex || null == content) { + return null; + } + + final Pattern pattern = PatternPool.get(regex, Pattern.DOTALL); + return allIndexOf(pattern, content); + } + + /** + * 找到指定模式匹配到所有字符串的位置 + * + * @param pattern 模式 + * @param content 字符串 + * @return 位置集合,{@code null}表示未找到 + * @since 5.6.34 + */ + public static List allIndexOf(Pattern pattern, CharSequence content) { + List results = null; + if (null != pattern && null != content) { + final Matcher matcher = pattern.matcher(content); + while (matcher.find()) { + if (results == null) { + results = new ArrayList<>(); + } + results.add(matcher.toMatchResult()); + } + } + + return results; + } + /** * 从字符串中获得第一个整数 *