"fnmatch" --- Unix filename pattern matching
********************************************

**Code source :** Lib/fnmatch.py

======================================================================

Ce module fournit la gestion des caractères de remplacement de style
shell Unix, qui ne sont *pas* identiques à ceux utilisés dans les
expressions régulières (documentés dans le module "re"). Les
caractères spéciaux utilisés comme caractères de remplacement de style
shell sont :

+--------------+--------------------------------------+
| Motif        | Signification                        |
|==============|======================================|
| "*"          | reconnaît n'importe quoi             |
+--------------+--------------------------------------+
| "?"          | reconnaît n'importe quel caractère   |
|              | unique                               |
+--------------+--------------------------------------+
| "[seq]"      | reconnaît n'importe quel caractère   |
|              | dans *seq*                           |
+--------------+--------------------------------------+
| "[!seq]"     | reconnaît n'importe quel caractère   |
|              | qui n'est pas dans *seq*             |
+--------------+--------------------------------------+

Pour une correspondance littérale, il faut entourer le métacaractère
par des crochets. Par exemple, "'[?]'" reconnaît le caractère "'?'".

Notons que le séparateur de nom de fichiers ("'/'" sous Unix) n'est
*pas* traité de manière spéciale par ce module. Voir le module "glob"
pour la recherche de chemins ("glob" utilise "filter()" pour
reconnaître les composants d'un chemin). De la même manière, les noms
de fichiers commençant par un point ne sont pas traités de manière
spéciale par ce module, et sont reconnus par les motifs "*" et "?".

Unless stated otherwise, "filename string" and "pattern string" either
refer to "str" or "ISO-8859-1" encoded "bytes" objects. Note that the
functions documented below do not allow to mix a "bytes" pattern with
a "str" filename, and vice-versa.

Finally, note that "functools.lru_cache()" with a *maxsize* of 32768
is used to cache the (typed) compiled regex patterns in the following
functions: "fnmatch()", "fnmatchcase()", "filter()".

fnmatch.fnmatch(name, pat)

   Test whether the filename string *name* matches the pattern string
   *pat*, returning "True" or "False".  Both parameters are case-
   normalized using "os.path.normcase()". "fnmatchcase()" can be used
   to perform a case-sensitive comparison, regardless of whether
   that's standard for the operating system.

   Cet exemple affiche tous les noms de fichiers du répertoire courant
   ayant pour extension ".txt" :

      import fnmatch
      import os

      for file in os.listdir('.'):
          if fnmatch.fnmatch(file, '*.txt'):
              print(file)

fnmatch.fnmatchcase(name, pat)

   Test whether the filename string *name* matches the pattern string
   *pat*, returning "True" or "False"; the comparison is case-
   sensitive and does not apply "os.path.normcase()".

fnmatch.filter(names, pat)

   Construct a list from those elements of the *iterable* of filename
   strings *names* that match the pattern string *pat*. It is the same
   as "[n for n in names if fnmatch(n, pat)]", but implemented more
   efficiently.

fnmatch.translate(pat)

   Return the shell-style pattern *pat* converted to a regular
   expression for using with "re.match()". The pattern is expected to
   be a "str".

   Exemple :

   >>> import fnmatch, re
   >>>
   >>> regex = fnmatch.translate('*.txt')
   >>> regex
   '(?s:.*\\.txt)\\Z'
   >>> reobj = re.compile(regex)
   >>> reobj.match('foobar.txt')
   <re.Match object; span=(0, 10), match='foobar.txt'>

Voir aussi:

  Module "glob"
     Recherche de chemins de style shell Unix.
