Skip to main content

Dynamically generate regex patterns

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, ormore=True)
host = Amount(Set(WORD, DIGIT, '.'), 1, ormore=True)
port = Optional(Group(RegexPattern(":") + Amount(DIGIT, 1, ormore=True)))
path = Amount(Group(RegexPattern('/') + Group(Amount(NotSet('/', '#', '?', '&', WHITESPACE), 0, ormore=True))), 0, ormore=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 use for it. You can also extend this library by subclassing RegexPattern and add your own support for different regex flavors.

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-0.0.0.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

RegexFactory-0.0.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: RegexFactory-0.0.0.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for RegexFactory-0.0.0.tar.gz
Algorithm Hash digest
SHA256 3335bc458854f112a31630eb4b9c684b84c4293e4adc380705346b7aa875b573
MD5 d146a9e8b142bb3a69c2e7a6f23b3c39
BLAKE2b-256 4d732edff8cb6d693476718a119bbd98606891045d38b017a32ca33d228dca37

See more details on using hashes here.

File details

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

File metadata

  • Download URL: RegexFactory-0.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for RegexFactory-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0895aad8ba426c7fec99e2929541d4bc5dd861bf94f696a76108c02d258dd1a2
MD5 e7c5e069ff2705a0331fc7f901a63093
BLAKE2b-256 612d50534945b65330334df20ae75e80bc33ee88c2cd926f0bd7a629eafc772b

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