Skip to main content

A library for managing regula expressions with templates

Project description

Replus v0.3.0

License: MIT codecov Documentation Status Build PyPI PyPI - Downloads

A wrapper for the regex library for advanced pattern management.

Quickstart

Installation

pip install replus

or clone this repo

git clone git@github.com:biagiodistefano/replus.git

and then run

python setup.py install

Template creation

The Engine loads Regular Expression pattern templates written in *.json files from the provided directory, builds and compiles them in the following fashion:

example of template patterns/date.json:

{
  "day": [
    "3[01]",
    "[12][0-9]",
    "0?[1-9]"
  ],
  "month": [
    "0?[1-9]",
    "1[012]"
  ],
  "year": [
    "\\d{4}"
  ],
  "date": [
    "{{day}}/{{month}}/{{year}}",
    "{{year}}-{{month}}-{{day}}"
  ],
  "$PATTERNS": [
    "{{date}}"
  ]
}

will result in the following regex:

(?P<date_0>(?P<day_0>[12][0-9]|0?[1-9]|3[01])/(?P<month_0>0?[1-9]|1[012])/(?P<year_0>\d{4})|(?P<year_1>\d{4})-(?P<month_1>0?[1-9]|1[012])-(?P<day_1>[12][0-9]|0?[1-9]|3[01]))

Only the patterns under $PATTERNS will be matched against at runtime.

Querying

It is possible to query as follows:

from replus import Replus

engine = Replus('patterns')

for match in engine.parse("Look at this date: 2012-20-10"):
    print(match)
    # <[Match date] span(19, 29): 2012-12-10>

    date = match.group('date')
    print(date)
    # <[Group date_0] span(19, 29): 2012-12-10>

    day = date.group('day')
    print(day)
    # <[Group day_1] span(27, 29): 10>

    month = date.group('month')
    print(month)
    # <[Group month_1] span(24, 26): 12>

    year = date.group('year')
    print(year)
    # [Group year_1] span(19, 23): 2012>

Filtering

it is possible to filter regexes by type, being the type given by the json's filename's stem. E.g., in the above example, results matched by the patterns under patterns/date.json's $PATTERNS will have type date

filters = ["dates", "cities"]
for match in engine.parse(my_string, *filters):
    # do stuff

Extra features

There are two useful secondary features:

  • non-capturing groups: these are specified by using the "?:" prefix in the group name or key
  • atomic groups: these are specified by using the "?>" prefix in the group name or key
  • dynamic backreferences: use # to reference a previous group and @<n> to specify how many groups behind

template:

{
  "?:number": [
    "\\d"
  ],
  "abg": [
    "alpha",
    "beta",
    "gamma"
  ],
  "spam": [
     "spam"
   ],
   "eggs": [
     "eggs"
   ],
  "patterns": [
    "This is an unnamed number group: {{number}}.",
    "I can match {{abg}} and {{abg}}, and then re-match the last {{#abg}} or the second last {{#abg@2}}",
    "Here is some {{?:spam}} and some {{?>eggs}}"
  ]
}

It will generate the following regexs:

This is an unnamed number group: (?:\d).
I can match (?P<abg_0>alpha|beta|gamma) and (?P<abg_1>alpha|beta|gamma), and then re-match the last (?P=abg_1) or the second last (?P=abg_0)
Here is some (?:spam) and some (?>eggs)

N.B.: in order to obtain an escape char, such as \d, in the pattern's model it must be double escaped: \\d

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

replus-0.3.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

replus-0.3.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file replus-0.3.0.tar.gz.

File metadata

  • Download URL: replus-0.3.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for replus-0.3.0.tar.gz
Algorithm Hash digest
SHA256 a716861eda59db3b60849df0f146d84adac1a68e64d10c244dfa6d0349ef3470
MD5 8d335a3ccda9c119173e792dc9e19c7c
BLAKE2b-256 7e161b25ef7cfac30f26c406dc0231d9f62f8ee3e286725cabdb556cec1b5d11

See more details on using hashes here.

File details

Details for the file replus-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: replus-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for replus-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 00f2d287aa6e488c35fe7e05846fdadd7f5c1e5d5b5173aba060155655aaa021
MD5 c32426659eda063de200cbce17cbd37e
BLAKE2b-256 a46b5a8ebd9f47348dd94041508f4613725f43acbe0c7b6f323b324d296c78fe

See more details on using hashes here.

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