Ant Path Style Patterns

How do I use Nant/Ant naming patterns?

The rules are:

  • a single star (*) matches zero or more characters within a path name
  • a double star (**) matches zero or more characters across directory levels
  • a question mark (?) matches exactly one character within a path name

Another way to think about it is double star (**) matches slash (/) but single star (*) does not.

Let's say you have the files:

  1. bar.txt
  2. src/bar.c
  3. src/baz.c
  4. src/test/bartest.c

Then the patterns:

  • *.c             matches nothing (there are no .c files in the current directory)
  • src/*.c     matches 2 and 3
  • */*.c         matches 2 and 3 (because * only matches one level)
  • **/*.c       matches 2, 3, and 4 (because ** matches any number of levels)
  • bar.*         matches 1
  • **/bar.*   matches 1 and 2
  • **/bar*.* matches 1, 2, and 4
  • src/ba?.c matches 2 and 3
       

Ant Pattern Matching - * vs. **

To match all files, in all directories (from the base directory and deeper)

**/*.nupkg

Will match

sample.nupkg
sample-2.nupkg
tmp/sample.nupkg
tmp/other.nupkg
other/new/sample.nupkg

** will match any directory (multiple directories deep).

*.nupkg will match any file with the nupkg extension. Or just * will match any file or any directory (but just a single directory deep).

PS: There is no Ant Pattern Tester.

Ant path style regex with alternation

Ant's filename selector works fine with | in the regex pattern.

<fileset id="testpath" dir="${parent.dir}">
<filename regex="/foo/me|/bar/me|/foobar/me" />
</fileset>

<echo>${toString:testpath}</echo>

https://ant.apache.org/manual/Types/selectors.html#filenameselect

Using ant patterns to match on path variable

How your ant pattern should be depends on what {id} can be. The {id} in your ant matcher is not directly related to your {id} in your RequestMapping annotation.

Spring Security just checks, with the ant matcher, whether the path matches the provided pattern. In your case {id} matches any string and stores the matched value in a URI template variable named id for later use.

To specify the what is considered an {id} you can provide an regex. If your id consists of digits your matcher could be "/noun1/noun2/{regex:\\d+}/**":

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
import org.springframework.util.AntPathMatcher;

class AntMatcherTest {

@Test
void antMatcher() {
AntPathMatcher antPathMatcher = new AntPathMatcher();
String pattern = "/noun1/noun2/{id:\\d+}/**";

assertFalse(antPathMatcher.match(pattern, "/noun1/noun2/noun3/verb5"));
assertTrue(antPathMatcher.match(pattern, "/noun1/noun2/1/verb1"));
assertTrue(antPathMatcher.match(pattern, "/noun1/noun2/2/verb2"));
assertTrue(antPathMatcher.match(pattern, "/noun1/noun2/3/verb3/verb4"));
}
}

Spring MockMVC redirectUrlPattern throws No Ant-style path pattern

A valid Ant path pattern for your requirements would look like:

CREATE_SUCCESS_URL = "/courses/{[0-9]*}/edit"

or

CREATE_SUCCESS_URL = "/courses/{\\d*}/edit"

This AssertionError is caused by the following method inside org.springframework.util.AntPathMatcher:

public boolean isPattern(String path) {
return (path.indexOf('*') != -1 || path.indexOf('?') != -1);
}

So any string containing * or ? will work.



Related Topics



Leave a reply



Submit