mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
!439 ReUtil.java 添加了 2 个支持[命名捕获组]的方法
Merge pull request !439 from churen/v5-dev
This commit is contained in:
@@ -8,6 +8,7 @@ import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ReUtilTest {
|
||||
@@ -163,4 +164,26 @@ public class ReUtilTest {
|
||||
"(.+?)省(.+?)市(.+?)区", "广东省深圳市南山区");
|
||||
Console.log(match);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getByGroupNameTest() {
|
||||
String content = "2021-10-11";
|
||||
String regex = "(?<year>\\d+)-(?<month>\\d+)-(?<day>\\d+)";
|
||||
String year = ReUtil.getByGroupName(regex, content, "year");
|
||||
Assert.assertEquals("2021", year);
|
||||
String month = ReUtil.getByGroupName(regex, content, "month");
|
||||
Assert.assertEquals("10", month);
|
||||
String day = ReUtil.getByGroupName(regex, content, "day");
|
||||
Assert.assertEquals("11", day);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAllGroupNamesTest() {
|
||||
String content = "2021-10-11";
|
||||
String regex = "(?<year>\\d+)-(?<month>\\d+)-(?<day>\\d+)";
|
||||
Map<String, String> map = ReUtil.getAllGroupNames(regex, content);
|
||||
Assert.assertEquals(map.get("year"), "2021");
|
||||
Assert.assertEquals(map.get("month"), "10");
|
||||
Assert.assertEquals(map.get("day"), "11");
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user