!1275 ReUtil工具类添加“allIndexOf”方法,用于找到指定模式匹配到所有字符串的位置

Merge pull request !1275 from CoCoTea/v6-dev-20241108
This commit is contained in:
Looly 2024-11-11 11:43:12 +00:00 committed by Gitee
commit bda2d7a1e5
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F

View File

@ -794,6 +794,46 @@ public class ReUtil {
return result;
}
/**
* 找到指定模式匹配到所有字符串的位置
*
* @param regex 正则
* @param content 字符串
* @return 位置集合{@code null}表示未找到
* @since 6.0.0
*/
public static List<MatchResult> 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<MatchResult> allIndexOf(Pattern pattern, CharSequence content) {
List<MatchResult> 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;
}
/**
* 从字符串中获得第一个整数
*