mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
ReUtil工具类添加“allIndexOf”方法,用于找到指定模式匹配到所有字符串的位置
This commit is contained in:
parent
d34f26b302
commit
177ab4732e
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从字符串中获得第一个整数
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user