跳至主要內容

Spring的AntPathMatcher是个好东西

postjavaspring小于 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"));
	}
}


版权申明

本站点所有内容,版权均归https://wenchao.renopen in new window所有,除非明确授权,否则禁止一切形式的转载协议

打赏

微信 支付宝

上次编辑于:
打赏
给作者赏一杯咖啡吧
您的支持将是我继续更新下去的动力
微信微信
支付宝支付宝