Skip to main content

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

Project description

AntPathMatcher

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

References

Spring framework - AntPathMatcher

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

antpathmatcher-1.0.1.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

antpathmatcher-1.0.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file antpathmatcher-1.0.1.tar.gz.

File metadata

  • Download URL: antpathmatcher-1.0.1.tar.gz
  • Upload date:
  • Size: 19.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for antpathmatcher-1.0.1.tar.gz
Algorithm Hash digest
SHA256 6968f161b47f7c4c46f8551fa971a18523ce01a51daf808521081377c30c17a6
MD5 d8e3a77fbb0430da6841a53798dbfc47
BLAKE2b-256 f30fa87c05bdb739d77b42b5284ec932358fe3aeb81b2d8b2330cc79f7db1f63

See more details on using hashes here.

File details

Details for the file antpathmatcher-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: antpathmatcher-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for antpathmatcher-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 42a8e03fa74e74b4a4c5fff366b8495d14a7428f20d08a5f9c57d7bc3c5cc66e
MD5 dae15dd94669e409c45ea5fb28dd652b
BLAKE2b-256 e774fffac0a3348b6662a3e76e94c3f50c8ce00b4ee56ba246f815478856605b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page