Spring的AntPathMatcher是个好东西
小于 1 分钟
Spring的AntPathMatcher是个好东西
经常需要在各种中做一些模式匹配,正则表达式虽然是个好东西,但是Ant风格
的匹配情况也非常的多。 这种情况下使用正则表达式不一定方便,而Spring提供的AntPathMatcher
确可以帮助我们简化很多。
位于Spring-core
中的org.springframework.util.AntPathMatcher
使用起来非常简单:
public class AntPathMatcherTest {
private AntPathMatcher pathMatcher = new AntPathMatcher();
@Test
public void test() {
pathMatcher.setCachePatterns(true);
pathMatcher.setCaseSensitive(true);
pathMatcher.setTrimTokens(true);
pathMatcher.setPathSeparator("/");
Assert.assertTrue(pathMatcher.match("a", "a"));
Assert.assertTrue(pathMatcher.match("a*", "ab"));
Assert.assertTrue(pathMatcher.match("a*/**/a", "ab/asdsa/a"));
Assert.assertTrue(pathMatcher.match("a*/**/a", "ab/asdsa/asdasd/a"));
Assert.assertTrue(pathMatcher.match("*", "a"));
Assert.assertTrue(pathMatcher.match("*/*", "a/a"));
}
}