How do I use Nant/Ant naming patterns?

AntNant

Ant Problem Overview


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?

Ant Solutions


Solution 1 - Ant

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    

Solution 2 - Ant

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

Solution 3 - Ant

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.

Solution 4 - Ant

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionberkoView Question on Stackoverflow
Solution 1 - AntbenzadoView Answer on Stackoverflow
Solution 2 - AntsparkplugView Answer on Stackoverflow
Solution 3 - AntAndy WhitfieldView Answer on Stackoverflow
Solution 4 - AntAditya Kumar ShrivastavaView Answer on Stackoverflow