Skip to main content

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

datetime_matcher-0.1.3.tar.gz (5.3 kB view hashes)

Uploaded Source

Built Distribution

datetime_matcher-0.1.3-py3-none-any.whl (5.9 kB view hashes)

Uploaded Python 3

Supported by

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