A library which extends regex with support for datetime format codes.
Project description
date-matcher
A python module that works with an extension of regex which allows formatted datetime strings using python/C-style strftime format codes, hereby dubbed "dfregex" (read datetime format regex).
Most notably, the library includes a function which can substitute a given text matching a given search dfregex with a replacement string, allowing both datetime format substitutions and backslash-number substitutions. See an example in the section below.
Install
Get it from pypi now by running.
pip install datetime-matcher
Substitution Example
Given the following as search pattern:
search_dfregex = r'(\w+)\%.+_(%Y-%b-%d)\.jpe?g'
Note: 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.
If we use this piece of text:
text = 'MyLovelyPicture%38E7F8AEA5_2020-Mar-10.jpeg'
And the following replacement formatter:
replacement = r'%Y%m%d-\1.jpg'
Then we can run the following substitution:
from datetime_matcher import DatetimeMatcher
dtmatcher = DatetimeMatcher()
result = dtmatcher.sub(search_dfregex, replacement, text)
# result == '20200310-MyLovelyPicture.jpg'
Features
The library features a class DatetimeMatcher
which provides the following
public-facing methods:
sub
def sub(self, search_dfregex: str, replacement_dfregex: str, text: str) -> str
- Replace the first matching instance of the search dfregex in the given text with the replacement dfregex, intelligently transferring the first matching date from the original text to the replaced text.
- Use strftime codes within a dfregex string to extract/place datetimes.
- Example: as demonstrated in the section above.
match
def match(self, search_dfregex: str, text: str) -> Optional[Match[AnyStr]]
- Determine if the given text matches the search dfregex, and return the corresponding Match object if it exists.
- Use strftime codes within a dfregex string to extract/place datetimes.
get_regex_from_dfregex
def get_regex_from_dfregex(self, dfregex: str, is_capture_dfs: bool = False) -> str
- Converts a dfregex to its corresponding conventional regex.
- By default, the datetime format groups are NOT captured.
- Use strftime codes within a dfregex string to match datetimes.
extract_datetime
def extract_datetime(self, dfregex: str, text: str) -> Optional[datetime]
- Extracts a datetime object from text given a dfregex string.
- Use strftime codes within a dfregex string to match datetimes.
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
File details
Details for the file datetime_matcher-0.1.3.tar.gz
.
File metadata
- Download URL: datetime_matcher-0.1.3.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 646c84f602530266901dbfaf466bad1385bd319534a9bb072243d55136062951 |
|
MD5 | 8257ad997dd6cfadc23a280d2beaf349 |
|
BLAKE2b-256 | bcaa49d3bdcb19b9a0daabc3c49e6c0a6221cd8e62caedb836a3408ba14bfb4d |
Provenance
File details
Details for the file datetime_matcher-0.1.3-py3-none-any.whl
.
File metadata
- Download URL: datetime_matcher-0.1.3-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2b17fc1feedf278d413a2c4290a7a67b51d1befb9f933440a3069703263d2692 |
|
MD5 | 5ca49d926b519a59768b9fb94542f53e |
|
BLAKE2b-256 | 925ecdebbad0fd6e8ff5ca6aa5f2907e5923e181d4a3460cf54fcfed2b0ba11b |