120

I have to admit that I always forgot the syntactical intracacies of the naming patterns for Nant (eg. those used in filesets). The double asterisk/single asterisk stuff seems to be very forgettable in my mind.

Can someone provide a definitive guide to the naming patterns?

aioobe
  • 413,195
  • 112
  • 811
  • 826
berko
  • 2,935
  • 4
  • 26
  • 31
  • 4
    I have read the reference. And re-read it. And re-read it. I just think it has a really poor set of examples and is unclear in its message. – berko Sep 17 '08 at 00:12
  • Maybe you can specify exactly what you're trying to achive and someone can provide a better answer? – Andy Whitfield Sep 17 '08 at 12:34

4 Answers4

288

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    
noisy
  • 6,495
  • 10
  • 50
  • 92
benzado
  • 82,288
  • 22
  • 110
  • 138
  • 9
    I think that this is a much better explanation than the Nant reference. Cheers! – berko Sep 18 '08 at 09:41
  • 1
    What does src/*/** match? I would expect it to be 4, but my experience with maven seems to indicate that you match any files in any folders you need src/*/*/** – Matthew Buckett Apr 02 '13 at 16:43
  • 3
    what would `**.c` match? – chharvey May 23 '15 at 03:01
  • 3
    I don't use Ant any more, so I'm not in a place to answer follow-up questions. You should ask a new question or, even better, find the answer and then submit an edit to my answer. – benzado Jun 03 '15 at 17:01
  • What if there is a space in the path? http://stackoverflow.com/questions/37731763/how-to-exclude-files-with-spaces-in-the-path-in-sonar – John Meyer Jun 10 '16 at 14:55
  • Can someone explain the rationale behind why `**/bar.*` matches `bar.txt`, which does not contain a slash? – sitaktif Sep 27 '16 at 03:38
  • 1
    @sitaktif One way to think about it is that `bar.txt` is implicitly `./bar.txt` (`.` means the current directory). So that list is really `./bar.txt`, `./src/bar.c`, etc. and the `./` is assumed. – benzado Sep 29 '16 at 15:28
  • How'd I match zero or one character, for example both `test.yaml` and `test.yml`? `test.y*ml` works but is too wide. – Abhijit Sarkar Dec 18 '17 at 01:46
  • @AbhijitSarkar I don't know! You should post a new question so that more people see it. This one is over nine years old! – benzado Dec 19 '17 at 03:59
  • The missing example: `**`. matches 1, 2, 3, and 4 – bobfoster Apr 23 '21 at 19:33
24

Here's a few extra pattern matches which are not so obvious from the documentation. Tested using NAnt for the example files in benzado's answer:

  1. bar.txt
  2. src/bar.c
  3. src/baz.c
  4. src/test/bartest.c
  • src**                      matches 2, 3 and 4
  • **.c                        matches 2, 3, and 4
  • **ar.*                    matches 1 and 2
  • **/bartest.c/**  matches 4
  • src/ba?.c/**        matches 2 and 3
mwfearnley
  • 3,303
  • 2
  • 34
  • 35
sparkplug
  • 1,317
  • 2
  • 15
  • 32
5

Double asterisks (**) are associated with the folder-names matching, whereas single symbols asterisk (* = multi characters) as well as the question-mark (? = single character) are used to match the file-names.

user
  • 86,916
  • 18
  • 197
  • 190
4

Check out the Nant reference. The fileset patterns are:

'*' matches zero or more characters, e.g. *.cs
'?' matches one character, e.g. ?.cs

And '**' matches a directory tree e.g. src/**/*.cs will find all cs files in any sub-directory of src.

Andy Whitfield
  • 2,373
  • 2
  • 19
  • 22