Skip to main content

AntPathMatcher

Python Version License: MIT Type Hints macOS Windows Linux

Python Implementation of the Ant-style path patterns, inspired by Spring Framework's AntPathMatcher.

🔗 References

🚀 Key Features

  • Match URL paths and file paths using Ant-style patterns
  • Support for wildcards:
    • ? - matches exactly one character (except path separators)
    • * - matches zero or more characters (except path separators)
    • ** - matches zero or more directories in a path
  • Extract variables from URI templates (e.g., /users/{id} matches /users/123 with id=123)
  • Customizable path separators (default is / but can be changed)
  • Fully type-hinted for modern Python development
  • Compatible with Python 3.10+

📦 Installation

pip install antpathmatcher

Or with uv:

uv add antpathmatcher

🎯 Quick Start

Basic Pattern Matching

from antpathmatcher import AntPathMatcher

# Create a matcher with default path separator (/)
matcher = AntPathMatcher()

# Basic matching with single character wildcard
matcher.match("com/t?st.jsp", "com/test.jsp")  # True
matcher.match("com/t?st.jsp", "com/tast.jsp")  # True
matcher.match("com/t?st.jsp", "com/txst.jsp")  # True
matcher.match("com/t?st.jsp", "com/tst.jsp")   # False (? requires exactly one character)

# Matching with * wildcard (matches within a path segment)
matcher.match("com/*.jsp", "com/demo.jsp")     # True
matcher.match("com/*.jsp", "com/test.jsp")     # True
matcher.match("com/*.jsp", "com/sub/test.jsp") # False (doesn't cross path boundaries)

# Matching with ** wildcard (matches across path segments)
matcher.match("com/**/test.jsp", "com/test.jsp")           # True
matcher.match("com/**/test.jsp", "com/sub/test.jsp")       # True
matcher.match("com/**/test.jsp", "com/sub/dir/test.jsp")   # True

# Complex patterns
matcher.match("org/demo/**/*.jsp", "org/demo/views/home.jsp")           # True
matcher.match("org/demo/**/*.jsp", "org/demo/templates/views/home.jsp") # True

Extracting URI Template Variables

from antpathmatcher import AntPathMatcher

matcher = AntPathMatcher()

# Basic variable extraction
variables = matcher.extract_uri_template_variables("/hotels/{hotel}", "/hotels/1")
print(variables)  # {'hotel': '1'}

# Multiple variables
variables = matcher.extract_uri_template_variables(
    "/users/{user_id}/posts/{post_id}", 
    "/users/123/posts/456"
)
print(variables)  # {'user_id': '123', 'post_id': '456'}

Custom Path Separators

from antpathmatcher import AntPathMatcher

# Create a matcher with custom path separator
# Useful for matching things like Java package names
matcher = AntPathMatcher(path_separator=".")

# Now the patterns use dots as separators
matcher.match("com.example.*.service", "com.example.user.service")  # True
matcher.match("com.example.???.service", "com.example.api.service") # True
matcher.match("com.**.service", "com.example.module.api.service")   # True

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

Metadata

Release files for antpathmatcher 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for antpathmatcher 1.0.2
File Size Uploaded
antpathmatcher-1.0.2.tar.gz 21.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for antpathmatcher 1.0.2
File Interpreter ABI Platform
antpathmatcher-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 28.3 kB

Release files / antpathmatcher-1.0.2.tar.gz

Download URL antpathmatcher-1.0.2.tar.gz
Size 21.9 kB
Tags Source
SHA-256 checksum
How to use checksums
6d418a19a40dc78c593b474e263757c1fe844e9fb601d758f500fabf2a51d7b9
BLAKE2b-256 checksum
How to use checksums
448adbdcfc59b61fe14d6da4705c12d5c3759e9c345a6f7f903b30b8dbef18c8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / antpathmatcher-1.0.2-py3-none-any.whl

Download URL antpathmatcher-1.0.2-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c376897f51a78c3ae3639d51f8c8dc17af80d920b5895d34d56089bf5456b510
BLAKE2b-256 checksum
How to use checksums
d19eb8b168e33273f2b9fb698b410885cde4b0e3e197f082b66ee202de34af4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page