Spring的AntPathMatcher是个好东西

经常需要在各种中做一些模式匹配,正则表达式虽然是个好东西,但是Ant风格的匹配情况也非常的多。
这种情况下使用正则表达式不一定方便,而Spring提供的AntPathMatcher确可以帮助我们简化很多。

位于Spring-core中的org.springframework.util.AntPathMatcher使用起来非常简单:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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"));
}
}
# spring

Commentaires

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×