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:
- bar.txt
- src/bar.c
- src/baz.c
- 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 4src/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
What's the Correct Way to Send a File from Rest Web Service to Client
How to Match "Any Character" in Regular Expression
How to Compile a Java Source File Which Is Encoded as "Utf-8"
Differences Between Java 8 Date Time API (Java.Time) and Joda-Time
Does Java Have a Int.Tryparse That Doesn't Throw an Exception for Bad Data
Sorting an Array of Int Using Bubblesort
How to Find and Kill Running Win-Processes from Within Java
Implementation Difference Between Aggregation and Composition in Java
If(False) VS. While(False): Unreachable Code VS. Dead Code
How to Write a Compareto Method Which Compares Objects
How to Change My Windows Desktop Wallpaper Programmatically in Java/Groovy
How to Test If My Font Is Rendered Correctly in PDF
This: Cannot Use This in Static Context
@Transactional Method Called from Another Method Doesn't Obtain a Transaction
Hibernate One-To-One: Getid() Without Fetching Entire Object