A library which extends regex with support for datetime format codes.
Project description
datetime-matcher 📆←💬
datetime-matcher is python module that enables an extension of regex which allows matching, extracting, and reformatting stringified datetimes.
It does so by providing an interface eerily similar to python's native re module.
It's mighty useful for doing things like bulk-renaming files with datetimes in their filenames. But don't let us tell you what it's good for—give it a try yourself!
Quick Links
- Getting Started — Quick introduction in the README.
- API Documentation — Wiki page with the latest documentation. Past versions will be archived.
- Dfregex Syntax Informal Spec — Informal specification for what is considered valid Dfregex.
- Developer's Guide — Quick guidelines on contributing.
Getting Started
Install it from pypi by running
pip install datetime-matcher
Then, get it into your code by importing and instantiating
from datetime_matcher import DatetimeMatcher
dtm = DatetimeMatcher()
Finally, run your data through it to perform subsitutions (or any of our many other supported operations!)
oh_my_would_you_look_at_the_time = [
'TheWallClock_1982-Feb-27.jpeg',
'TheWristWatch_2003-Aug-11.jpg',
'TheSmartWatch_2020-Mar-10.jpeg',
]
pattern = r'(\w+)_%Y-%b-%d\.jpe?g'
replace = r'%Y%m%d-\1.jpg'
its_all_clear_now = dtm.sub(pattern, replace, text) for text in oh_my_would_you_look_at_the_time
assert its_all_clear_now[0] == '19820227-TheWallClock.jpg'
assert its_all_clear_now[1] == '20030811-TheWristWatch.jpg'
assert its_all_clear_now[2] == '20200310-TheSmartWatch.jpg'
Example
Use Case in String Substitution with Datetime Reformatting
Let's say we have several filenames of the following format that we want to rename:
'MyLovelyPicture_2020-Mar-10.jpeg'
We want to change them to look like this string:
'20200310-MyLovelyPicture.jpg'
The Unclean Way to Do It, without datetime-matcher
Using the standard library re.sub, we run into an issue:
text = 'MyLovelyPicture_2020-Mar-10.jpeg'
pattern = r'(\w+)_([0-9]{4}-\w{3}-[0-9]{2})\.jpe?g' # ❌ messy
replace = r'(??????)-\1.jpg' # ❌ what do we put for ??????
result = re.sub(pattern, replace, text) # ❌ This does't work
We have to manually run datetime.strptime with a custom parser string to extract the
date, and then manually insert it back into the replacement string before running
a non-generic search-and-replace using the customized replacement string.
Yuck.
The Clean Way to Do It, with datetime-matcher
We can do the following for a quick and easy substitution with reformatting.
from datetime_matcher import DatetimeMatcher
dtmatcher = DatetimeMatcher()
text = 'MyLovelyPicture_2020-Mar-10.jpeg'
pattern = r'(\w+)_%Y-%b-%d\.jpe?g' # ✅ regex + strptime
replace = r'%Y%m%d-\1.jpg' # ✅ template + strftime
result = dtmatcher.sub(pattern, replace, text) # ✅ magical substitution
assert result == '20200310-MyLovelyPicture.jpg' # ✅ This works like a charm
Dfregex Syntax Informal Spec
The syntax for dfregex is nearly identical to that of conventional python regex. There is only one addition and one alteration to support datetime format codes. This is far from a formal spec, but expect that currently supported syntaxes, within the current major semantic version, will NOT be removed unless provided reasonable notification and a generous deprecation period.
The Datetime Format Codes
The percentage character indicates the beginning of a datetime format code. These codes
are the standard C-style ones used in the built-in datetime module for strftime.
For a list of standard codes, see the Python docs.
Minus the exceptions below, and barring platform-specific support, strftime.org is a good alternative list.
NOTE: The following codes are currently not supported:
%Z,%c,%x,%X
The Percent Literal (%)
The percentage literal in conventional regex (%) must be escaped in dfregex (\%)
because an unescaped one marks the beginning of a datetime format code and otherwise would be
ambiguous.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file datetime_matcher-0.3.0.tar.gz.
File metadata
- Download URL: datetime_matcher-0.3.0.tar.gz
- Upload date:
- Size: 13.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4f22e3a5182bc20d94af85610f6944143e9fceb2cd74a3ddf39f21b7b2c1613
|
|
| MD5 |
d2a747ed395330ddb9c26a44f88f56b8
|
|
| BLAKE2b-256 |
aadb339e0fa148ba70ba1327e11d605a5723696e493788bcf5454593f46b400d
|
Provenance
The following attestation bundles were made for datetime_matcher-0.3.0.tar.gz:
Publisher:
publish.yml on stephen-zhao/datetime_matcher
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
datetime_matcher-0.3.0.tar.gz -
Subject digest:
d4f22e3a5182bc20d94af85610f6944143e9fceb2cd74a3ddf39f21b7b2c1613 - Sigstore transparency entry: 716818280
- Sigstore integration time:
-
Permalink:
stephen-zhao/datetime_matcher@a94e5a0bac8378d7c7bb263652804251299cd30f -
Branch / Tag:
refs/heads/main - Owner: https://github.com/stephen-zhao
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a94e5a0bac8378d7c7bb263652804251299cd30f -
Trigger Event:
workflow_run
-
Statement type:
File details
Details for the file datetime_matcher-0.3.0-py3-none-any.whl.
File metadata
- Download URL: datetime_matcher-0.3.0-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
378c11385f602719c07e3daa430b8beda6cc8ec02476a9fd40627e8ae05ef80d
|
|
| MD5 |
b7ba297fa35273396cdc991849ec9d71
|
|
| BLAKE2b-256 |
d21d1a541dc166822b08da7547571b416dfff0e3ed144f1a3e2a838530b34e3e
|
Provenance
The following attestation bundles were made for datetime_matcher-0.3.0-py3-none-any.whl:
Publisher:
publish.yml on stephen-zhao/datetime_matcher
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
datetime_matcher-0.3.0-py3-none-any.whl -
Subject digest:
378c11385f602719c07e3daa430b8beda6cc8ec02476a9fd40627e8ae05ef80d - Sigstore transparency entry: 716818289
- Sigstore integration time:
-
Permalink:
stephen-zhao/datetime_matcher@a94e5a0bac8378d7c7bb263652804251299cd30f -
Branch / Tag:
refs/heads/main - Owner: https://github.com/stephen-zhao
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a94e5a0bac8378d7c7bb263652804251299cd30f -
Trigger Event:
workflow_run
-
Statement type: