Regular expressions are powerful and notoriously hard to write correctly. Here's how to test, explain, and generate regex patterns via API — without memorizing syntax.
The Three Problems with Regex
- Writing: Syntax differs between languages. JavaScript, Python, and Go have subtle incompatibilities.
- Testing: You need sample data to verify edge cases.
-
Reading: Six months later,
/^(?:[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:.[a-z0-9!#$%&'+/=?^_{|}~-]+))/` means nothing.
Pattern Testing via API
`python
import requests
Test a regex against multiple strings at once
resp = requests.post("https://api.lazy-mac.com/regex-toolkit/test", json={
"pattern": r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$",
"flags": "i",
"test_cases": [
"user@example.com",
"user+tag@subdomain.example.co.uk",
"not-an-email",
"missing@tld",
"@nodomain.com"
]
})
for result in resp.json()['results']:
print(f"{'✅' if result['match'] else '❌'} {result['input']}")
`
Output:
plaintext
✅ user@example.com
✅ user+tag@subdomain.example.co.uk
❌ not-an-email
❌ missing@tld
❌ @nodomain.com
Pattern Explanation
`python
resp = requests.post("https://api.lazy-mac.com/regex-toolkit/explain", json={
"pattern": r"\b\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\b"
})
print(resp.json()['explanation'])
"Matches an IPv4 address:
\b - word boundary
\d{1,3} - 1 to 3 digits
. - literal dot
(repeated 4 times with dots between)
\b - word boundary"
`
Pattern Generation
`python
resp = requests.post("https://api.lazy-mac.com/regex-toolkit/generate", json={
"description": "Korean phone number format: 010-XXXX-XXXX or 02-XXX-XXXX",
"examples": ["010-1234-5678", "02-123-4567"],
"counter_examples": ["123-456-7890", "010-12-5678"]
})
print(resp.json()['pattern'])
"^(010-\d{4}-\d{4}|02-\d{3}-\d{4})$"
`
Top comments (0)