Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/grandmoff100/regexfactory
Dynamically construct regex patterns with native python representations
https://github.com/grandmoff100/regexfactory
library python regex regex-generator regex-pattern regular-expression sphinx-documentation
Last synced: 3 months ago
JSON representation
Dynamically construct regex patterns with native python representations
- Host: GitHub
- URL: https://github.com/grandmoff100/regexfactory
- Owner: GrandMoff100
- License: gpl-3.0
- Created: 2021-10-24T21:36:03.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-05-29T01:50:26.000Z (8 months ago)
- Last Synced: 2024-10-14T08:49:56.871Z (4 months ago)
- Topics: library, python, regex, regex-generator, regex-pattern, regular-expression, sphinx-documentation
- Language: Python
- Homepage: https://regexfactory.readthedocs.io
- Size: 154 KB
- Stars: 7
- Watchers: 2
- Forks: 4
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# RegexFactory
Dynamically construct python regex patterns.
## Examples
### Matching Initials
Say you want a regex pattern to match the initials of someones name.
```python
import re
from regexfactory import Amount, Rangepattern = Amount(Range("A", "Z"), 2, 3)
matches = pattern.findall(
"My initials are BDP. Valorie's are VO"
)print(matches)
``````bash
['BDP', 'VO']
```### Matching Hex Strings
Or how matching both uppercase and lowercase hex strings in a sentence.
```python
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 = pattern.findall(sentence)
print(matches)
``````bash
['#000000', '5fb8a0', '#FF21FF']
```### Matching URLs
Or what if you want to match urls in html content?
```python
from regexfactory import *protocol = Amount(Range("a", "z"), 1, or_more=True)
host = Amount(Set(WORD, DIGIT, '.'), 1, or_more=True)
port = Optional(IfBehind(":") + Multi(DIGIT))
path = Multi(
RegexPattern('/') + Multi(
NotSet('/', '#', '?', '&', WHITESPACE),
match_zero=True
),
match_zero=True
)
patt = protocol + RegexPattern("://") + host + port + pathsentence = "This is a cool url, https://github.com/GrandMoff100/RegexFactory/ "
print(patt)print(patt.search(sentence))
``````bash
[a-z]{1,}://[\w\d.]{1,}(?:\d{1,})?(/([^/#?&\s]{0,})){0,}```
## 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?