# 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, Range
pattern = 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 + path
sentence = "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,}
<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?
Raw data
{
"_id": null,
"home_page": null,
"name": "RegexFactory",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "regex, generator, regular expression, strings",
"author": "GrandMoff100",
"author_email": "nlarsen23.student@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/f4/4d/6f7e066227c0baa97a579595a0af71ac3f414da69aec35792d3cceecd2fa/regexfactory-1.0.1.tar.gz",
"platform": null,
"description": "# RegexFactory\n\nDynamically construct python regex patterns.\n\n## Examples\n\n### Matching Initials\n\nSay you want a regex pattern to match the initials of someones name.\n\n```python\nimport re\nfrom regexfactory import Amount, Range\n\n\npattern = Amount(Range(\"A\", \"Z\"), 2, 3)\n\nmatches = pattern.findall(\n \"My initials are BDP. Valorie's are VO\"\n)\n\nprint(matches)\n```\n\n```bash\n['BDP', 'VO']\n```\n\n### Matching Hex Strings\n\nOr how matching both uppercase and lowercase hex strings in a sentence.\n\n```python\nimport re\nfrom regexfactory import *\n\npattern = Optional(\"#\") + Or(\n Amount(\n Set(\n Range(\"0\", \"9\"),\n Range(\"a\", \"f\")\n ),\n 6\n ),\n Amount(\n Set(\n Range(\"0\", \"9\"),\n Range(\"A\", \"F\")\n ),\n 6\n ),\n\n)\n\nsentence = \"\"\"\nMy favorite color is #000000. I also like 5fb8a0. My second favorite color is #FF21FF.\n\"\"\"\n\nmatches = pattern.findall(sentence)\nprint(matches)\n```\n\n```bash\n['#000000', '5fb8a0', '#FF21FF']\n```\n\n### Matching URLs\n\nOr what if you want to match urls in html content?\n\n```python\nfrom regexfactory import *\n\n\nprotocol = Amount(Range(\"a\", \"z\"), 1, or_more=True)\nhost = Amount(Set(WORD, DIGIT, '.'), 1, or_more=True)\nport = Optional(IfBehind(\":\") + Multi(DIGIT))\npath = Multi(\n RegexPattern('/') + Multi(\n NotSet('/', '#', '?', '&', WHITESPACE),\n match_zero=True\n ),\n match_zero=True\n)\npatt = protocol + RegexPattern(\"://\") + host + port + path\n\n\n\nsentence = \"This is a cool url, https://github.com/GrandMoff100/RegexFactory/ \"\nprint(patt)\n\nprint(patt.search(sentence))\n```\n\n```bash\n[a-z]{1,}://[\\w\\d.]{1,}(?:\\d{1,})?(/([^/#?&\\s]{0,})){0,}\n<re.Match object; span=(15, 51), match='https://github.com/GrandMoff100/RegexFactory/'>\n```\n\n## The Pitch\n\nThis library is really good at allowing you to intuitively understand how to construct a regex expression.\nIt helps you identify what exactly your regular expression is, and can help you debug it.\nThis is library is also very helpful for generating regex expressions on the fly if you find uses for it.\nYou can also extend this library by subclassing `RegexPattern` and add your own support for different regex flavors.\nLike generating regex expresison with Perl5 extensions.\n\nThere you have it. This library is intuitive, extensible, modular, and dynamic.\nWhy not use it?\n",
"bugtrack_url": null,
"license": "GPLv3",
"summary": null,
"version": "1.0.1",
"project_urls": null,
"split_keywords": [
"regex",
" generator",
" regular expression",
" strings"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "44d4c9e1f708f741ccf40a7c896c8e58774ddcbe788ffaf0d80b3697d7528b5f",
"md5": "2e7ec838e3a5ab9cc000b49fef51b3a2",
"sha256": "09d02e8570fb6922b6523dbdd757a261c0570d7b07671efca389663d9f8e5bee"
},
"downloads": -1,
"filename": "RegexFactory-1.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2e7ec838e3a5ab9cc000b49fef51b3a2",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 22025,
"upload_time": "2024-04-16T15:44:00",
"upload_time_iso_8601": "2024-04-16T15:44:00.365416Z",
"url": "https://files.pythonhosted.org/packages/44/d4/c9e1f708f741ccf40a7c896c8e58774ddcbe788ffaf0d80b3697d7528b5f/RegexFactory-1.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "f44d6f7e066227c0baa97a579595a0af71ac3f414da69aec35792d3cceecd2fa",
"md5": "fe5abc640b0913568426ddde96c3b29a",
"sha256": "8a3bc383f665e3205f5127c07ec8abffdcdff80de170b80319bbab7584cbfd87"
},
"downloads": -1,
"filename": "regexfactory-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "fe5abc640b0913568426ddde96c3b29a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 22705,
"upload_time": "2024-04-16T15:44:02",
"upload_time_iso_8601": "2024-04-16T15:44:02.055540Z",
"url": "https://files.pythonhosted.org/packages/f4/4d/6f7e066227c0baa97a579595a0af71ac3f414da69aec35792d3cceecd2fa/regexfactory-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-04-16 15:44:02",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "regexfactory"
}