Include-Exclude Pattern Matching GuideΒΆ
IntroductionΒΆ
This document explains the pattern matching syntax used in include-exclude patterns, which follows the same syntax as .gitignore
files. These patterns are used to specify which files to include or exclude when creating knowledge bases or other file filtering operations.
The pattern syntax allows for powerful and flexible matching of file paths using wildcards, directory specifiers, and character classes. This guide demonstrates each pattern type with concrete examples to help you understand exactly how they work.
Basic File Pattern MatchingΒΆ
Basic patterns match files regardless of their location in the directory structure.
Pattern |
Description |
Example Path |
Matches? |
---|---|---|---|
|
Matches any file named README.md in any directory |
README.md |
β Yes |
|
Matches README.md in subdirectories too |
folder/README.md |
β Yes |
|
Matches at any depth in the directory structure |
folder/subfolder/README.md |
β Yes |
|
Matches any Python file in any directory |
example.py |
β Yes |
|
Matches Python files in subdirectories |
folder/example.py |
β Yes |
|
Matches Python files at any depth |
folder/subfolder/example.py |
β Yes |
Directory-Specific MatchingΒΆ
Patterns can be made directory-specific to match files only in certain locations.
Pattern |
Description |
Example Path |
Matches? |
---|---|---|---|
|
Matches Python files directly in the src directory |
src/example.py |
β Yes |
|
Does NOT match Python files in other directories |
example.py |
β No |
|
Does NOT match Python files in other directories |
folder/example.py |
β No |
|
Does NOT match Python files in subdirectories of src |
src/folder/example.py |
β No |
Recursive Directory MatchingΒΆ
The **
pattern allows matching files recursively through directories.
Pattern |
Description |
Example Path |
Matches? |
---|---|---|---|
|
Matches Python files directly in the src directory |
src/example.py |
β Yes |
|
Matches Python files in subdirectories of src |
src/folder/example.py |
β Yes |
|
Matches Python files at any depth under src |
src/folder/subfolder/example.py |
β Yes |
|
Matches index.rst files at least 2 levels deep under docs/source |
docs/source/Section-1/index.rst |
β Yes |
|
Matches index.rst files at deeper levels |
docs/source/Section-1/Section-1-1/index.rst |
β Yes |
|
Does NOT match index.rst directly in docs/source |
docs/source/index.rst |
β No |
Directory Name MatchingΒΆ
Patterns can match directories and their contents.
Pattern |
Description |
Example Path |
Matches? |
---|---|---|---|
|
Matches files inside any directory named tmp |
tmp/file.txt |
β Yes |
|
Matches files in subdirectories of tmp |
tmp/folder/file.txt |
β Yes |
|
Matches files at any depth under tmp |
tmp/folder/subfolder/file.txt |
β Yes |
|
Matches files in tmp directories anywhere in the path |
tests/tmp/file.txt |
β Yes |
|
Matches files in nested tmp directories |
tests/tmp/folder/file.txt |
β Yes |
|
Matches deeply nested files in tmp directories |
tests/tmp/subfolder/file.txt |
β Yes |
Directory Contents MatchingΒΆ
Adding a trailing slash focuses on directory contents rather than the directory itself.
Pattern |
Description |
Example Path |
Matches? |
---|---|---|---|
|
Does NOT match the tmp directory itself |
tmp |
β No |
|
Matches files inside any tmp directory |
tmp/file.txt |
β Yes |
|
Matches files in subdirectories of tmp |
tmp/folder/file.txt |
β Yes |
|
Matches files at any depth under tmp |
tmp/folder/subfolder/file.txt |
β Yes |
|
Matches files in tmp directories anywhere in the path |
tests/tmp/file.txt |
β Yes |
|
Matches files in nested tmp directories |
tests/tmp/folder/file.txt |
β Yes |
|
Matches deeply nested files in tmp directories |
tests/tmp/subfolder/file.txt |
β Yes |
Character Class MatchingΒΆ
Character classes allow matching one character from a set of characters.
Pattern |
Description |
Example Path |
Matches? |
---|---|---|---|
|
Matches Python bytecode files (.pyc) |
test.pyc |
β Yes |
|
Matches Python optimized bytecode files (.pyo) |
test.pyo |
β Yes |
|
Matches Python dynamic library files (.pyd) |
test.pyd |
β Yes |
Summary of Pattern SyntaxΒΆ
Hereβs a quick reference for the pattern syntax demonstrated:
Pattern Element |
Description |
---|---|
|
Matches any sequence of characters within a path segment (not including path separators) |
|
Matches any sequence of characters spanning multiple path segments (including path separators) |
|
When used at the end of a pattern, specifies matching directory contents rather than the directory itself |
|
Character class that matches any single character from the set (a, b, or c) |
|
Specifies a directory prefix, restricting matches to that directory |
|
Specifies a directory prefix with recursive matching, finding matches in that directory and all its subdirectories |
These patterns can be combined to create powerful file selection rules for your knowledge base configuration. Use the examples above as a reference when creating your own patterns to ensure they match exactly the files you intend.