Skip to main content

PRegEx - Programmable Regular Expressions

Project description

Contributors MIT License

PRegEx - Programmable Regular Expressions

PRegEx is a Python package that can be used in order to construct Regular Expression patterns in a more human-friendly way.

Installation

You can start using PRegEx by installing it via pip:

pip install pregex

Usage

In PRegEx, everything is a Programmable Regular Expression, or "Pregex" for short. This makes it easy for simple Pregex instances to be combined into more complex ones! Within the code snippet below, we construct a Pregex instance that will match any URL that ends with either ".com" or ".org" as well as any IP address for which a 4-digit port number is specified. Furthermore, in the case that a match is a URL, its domain name will be separately captured as well.

from pregex.quantifiers import Optional, AtLeastOnce, AtLeastAtMost
from pregex.classes import AnyFrom, AnyDigit, AnyWhitespace
from pregex.groups import CapturingGroup
from pregex.tokens import Backslash
from pregex.operators import Either
from pregex.pre import Pregex

pre: Pregex = \
        Optional("http" + Optional('s') + "://") + \
        Optional("www.") + \
        Either(
            CapturingGroup(
                AtLeastOnce(~ (AnyWhitespace() | AnyFrom(":", Backslash())))
            ) +
            Either(".com", ".org"),

            3 * (AtLeastAtMost(AnyDigit(), min=1, max=3) + ".") +
            1 * AtLeastAtMost(AnyDigit(), min=1, max=3) +
            ":" + 4 * AnyDigit() 
        )

We can then easily fetch the resulting Pregex instance's underlying RegEx pattern.

regex = pre.get_pattern()

This is what that the above method returns. Yikes!

(?:https?\:\/\/)?(?:www\.)?(?:([^\\\s\:]+)(?:\.com|\.org)|(?:[\d]{1,3}\.){3}[\d]{1,3}\:[\d]{4})

Besides from having access to its underlying pattern, we can use a Pregex instance to find matches within a string. Consider for example the following piece of text:

text = "text--192.168.1.1:8000--text--http://www.wikipedia.orghttps://youtube.com--text"

We can scan the above string for any possible matches by invoking the instance's "get_matches" method:

matches = pre.get_matches(text)

Looks like there were three matches:

['192.168.1.1:8000', 'http://www.wikipedia.org', 'https://youtube.com']

Likewise, we can invoke the instance's "get_groups" method to get any captured groups.

groups = pre.get_groups(text)

As expected, there were only two captured groups as the first match is not a URL and therefore there did not exist a domain name to capture.

[(None,), ('wikipedia',), ('youtube',)]

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

pregex-1.0.3.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

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

pregex-1.0.3-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file pregex-1.0.3.tar.gz.

File metadata

  • Download URL: pregex-1.0.3.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for pregex-1.0.3.tar.gz
Algorithm Hash digest
SHA256 b06db8eac6b2cfaf79b3a240cf1f37f1da361b2346ceeb9943c0287716fae4e7
MD5 d46c8c5a065ebed22f0918976e97e22a
BLAKE2b-256 21d4f28486b1051ac6ab5f107184a9dc1b84e63f8dcb7fc0f4f00484cb425f2b

See more details on using hashes here.

File details

Details for the file pregex-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: pregex-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.5

File hashes

Hashes for pregex-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c97f7c34c690c1834efb9a9765dc956e3a2462fa5fcc5905b7d722681b33fd4b
MD5 987f2c29308b9ee2e5456527499a8e71
BLAKE2b-256 adb3a82dbeac40624ae92063a5af9d24a8c4f40134581444f5ec60f2f2689813

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