!439 ReUtil.java 添加了 2 个支持[命名捕获组]的方法

Merge pull request !439 from churen/v5-dev
This commit is contained in:
Looly
2021-10-14 15:45:49 +00:00
committed by Gitee
2 changed files with 80 additions and 0 deletions

View File

@@ -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");
}
}