Skip to main content

No project description provided

Project description

RegexFactory

Dynamically construct python regex patterns.

Examples

Matching Initials

Say you want a regex pattern to match the initials of someones name.

import re
from regexfactory import Amount, Range


pattern = Amount(Range("A", "Z"), 2, 3)

matches = re.findall(
    str(pattern),
    "My initials are BDP. Valorie's are VO"
)

print(matches)
['BDP', 'VO']

Matching Hex Strings

Or how matching both uppercase and lowercase hex strings in a sentence.

import re
from regexfactory import *

pattern = Optional("#") + Or(
    Amount(
        Set(
            Range("0", "9"),
            Range("a", "f")
        ),
        6
    ),
    Amount(
        Set(
            Range("0", "9"),
            Range("A", "F")
        ),
        6
    ),

)

sentence = """
My favorite color is #000000. I also like 5fb8a0. My second favorite color is #FF21FF.
"""

matches = re.findall(
    str(pattern),
    sentence
)
print(matches)
['#000000', '5fb8a0', '#FF21FF']

Matching URLs

Or what if you want to match urls in html content?

from regexfactory import *
import re


protocol = Amount(Range("a", "z"), 1, or_more=True)
host = Amount(Set(WORD, DIGIT, '.'), 1, or_more=True)
port = Optional(Group(RegexPattern(":") + Amount(DIGIT, 1, or_more=True)))
path = Amount(Group(RegexPattern('/') + Group(Amount(NotSet('/', '#', '?', '&', WHITESPACE), 0, or_more=True))), 0, or_more=True)
patt = protocol + RegexPattern("://") + host + port + path



sentence = "This is a cool url, https://github.com/GrandMoff100/RegexFactory/ "
print(patt)

print(re.search(str(patt), sentence))
[a-z]{1,}://[\w\d.]{1,}(:\d{1,})?(/([^/#?&\s]{0,})){0,}
<re.Match object; span=(15, 51), match='https://github.com/GrandMoff100/RegexFactory/'>

The Pitch

This library is really good at allowing you to intuitively understand how to construct a regex expression. It helps you identify what exactly your regular expression is, and can help you debug it. This is library is also very helpful for generating regex expressions on the fly if you find uses for it. You can also extend this library by subclassing RegexPattern and add your own support for different regex flavors. Like generating regex expresison with Perl5 extensions.

There you have it. This library is intuitive, extensible, modular, and dynamic. Why not use it?

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

RegexFactory-1.0.0.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

RegexFactory-1.0.0-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file RegexFactory-1.0.0.tar.gz.

File metadata

  • Download URL: RegexFactory-1.0.0.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for RegexFactory-1.0.0.tar.gz
Algorithm Hash digest
SHA256 42b435abc003268edad9896e98503542386f8ea1bbdbb0ee4eb0a5f8446b5e31
MD5 aed2a94afb184da271bcc5603fa9ff42
BLAKE2b-256 16a32c392f0b7b0fc92b162abdfd398f86c3127211ed08765f174f7705ef8340

See more details on using hashes here.

File details

Details for the file RegexFactory-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: RegexFactory-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for RegexFactory-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 70e523b567bd0e332639ed92fa9ea05105b539b2821b7e91b45693275473177a
MD5 60721c1884c969c6fb01fa6f5b98555c
BLAKE2b-256 cfc57ac9cdbce7edd87771e469220f8442ba807c0649c68fb966f1c80250db13

See more details on using hashes here.

Supported by

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