mirror of
https://gitee.com/dromara/hutool.git
synced 2025-06-28 13:34:09 +08:00
ReUtil增加getAllGroups重载
This commit is contained in:
parent
eb9df251d5
commit
5d500f8f14
@ -19,6 +19,7 @@
|
|||||||
* 【core 】 修复Dict#containsKey方法没区分大小写问题(pr#697@Gitee)
|
* 【core 】 修复Dict#containsKey方法没区分大小写问题(pr#697@Gitee)
|
||||||
* 【core 】 增加比较两个LocalDateTime是否为同一天(pr#693@Gitee)
|
* 【core 】 增加比较两个LocalDateTime是否为同一天(pr#693@Gitee)
|
||||||
* 【core 】 增加TemporalAccessorUtil.isIn、LocalDateTimeUtil.isIn(issue#I5HBL0@Gitee)
|
* 【core 】 增加TemporalAccessorUtil.isIn、LocalDateTimeUtil.isIn(issue#I5HBL0@Gitee)
|
||||||
|
* 【core 】 ReUtil增加getAllGroups重载(pr#2455@Github)
|
||||||
*
|
*
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
* 【core 】 修复CollUtil里面关于可变参数传null造成的crash问题(pr#2428@Github)
|
* 【core 】 修复CollUtil里面关于可变参数传null造成的crash问题(pr#2428@Github)
|
||||||
|
@ -1021,7 +1021,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
|||||||
* 获取数组中指定多个下标元素值,组成新数组
|
* 获取数组中指定多个下标元素值,组成新数组
|
||||||
*
|
*
|
||||||
* @param <T> 数组元素类型
|
* @param <T> 数组元素类型
|
||||||
* @param array 数组
|
* @param array 数组,如果提供为{@code null}则返回{@code null}
|
||||||
* @param indexes 下标列表
|
* @param indexes 下标列表
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
@ -1029,10 +1029,13 @@ public class ArrayUtil extends PrimitiveArrayUtil {
|
|||||||
if (null == array) {
|
if (null == array) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
if(null == indexes){
|
||||||
|
return newArray(array.getClass().getComponentType(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
final T[] result = newArray(array.getClass().getComponentType(), indexes.length);
|
final T[] result = newArray(array.getClass().getComponentType(), indexes.length);
|
||||||
for (int i : indexes) {
|
for (int i = 0; i < indexes.length; i++) {
|
||||||
result[i] = get(array, i);
|
result[i] = ArrayUtil.get(array, indexes[i]);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -206,18 +206,36 @@ public class ReUtil {
|
|||||||
* @since 4.0.13
|
* @since 4.0.13
|
||||||
*/
|
*/
|
||||||
public static List<String> getAllGroups(Pattern pattern, CharSequence content, boolean withGroup0) {
|
public static List<String> getAllGroups(Pattern pattern, CharSequence content, boolean withGroup0) {
|
||||||
|
return getAllGroups(pattern, content, withGroup0, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获得匹配的字符串匹配到的所有分组
|
||||||
|
*
|
||||||
|
* @param pattern 编译后的正则模式
|
||||||
|
* @param content 被匹配的内容
|
||||||
|
* @param withGroup0 是否包括分组0,此分组表示全匹配的信息
|
||||||
|
* @param findAll 是否查找所有匹配到的内容,{@code false}表示只读取第一个匹配到的内容
|
||||||
|
* @return 匹配后得到的字符串数组,按照分组顺序依次列出,未匹配到返回空列表,任何一个参数为null返回null
|
||||||
|
* @since 4.0.13
|
||||||
|
*/
|
||||||
|
public static List<String> getAllGroups(Pattern pattern, CharSequence content, boolean withGroup0, boolean findAll) {
|
||||||
if (null == content || null == pattern) {
|
if (null == content || null == pattern) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
ArrayList<String> result = new ArrayList<>();
|
ArrayList<String> result = new ArrayList<>();
|
||||||
final Matcher matcher = pattern.matcher(content);
|
final Matcher matcher = pattern.matcher(content);
|
||||||
if (matcher.find()) {
|
while (matcher.find()) {
|
||||||
final int startGroup = withGroup0 ? 0 : 1;
|
final int startGroup = withGroup0 ? 0 : 1;
|
||||||
final int groupCount = matcher.groupCount();
|
final int groupCount = matcher.groupCount();
|
||||||
for (int i = startGroup; i <= groupCount; i++) {
|
for (int i = startGroup; i <= groupCount; i++) {
|
||||||
result.add(matcher.group(i));
|
result.add(matcher.group(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(false == findAll){
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -534,4 +534,13 @@ public class ArrayUtilTest {
|
|||||||
String[] newArr = ArrayUtil.setOrAppend(arr, 0, "Good");// ClassCastException
|
String[] newArr = ArrayUtil.setOrAppend(arr, 0, "Good");// ClassCastException
|
||||||
Assert.assertArrayEquals(new String[]{"Good"}, newArr);
|
Assert.assertArrayEquals(new String[]{"Good"}, newArr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void getAnyTest() {
|
||||||
|
final String[] a = {"a", "b", "c", "d", "e"};
|
||||||
|
final Object o = ArrayUtil.getAny(a, 3, 4);
|
||||||
|
final String[] resultO = (String[]) o;
|
||||||
|
final String[] c = {"d", "e"};
|
||||||
|
Assert.assertTrue(ArrayUtil.containsAll(c, resultO[0], resultO[1]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user