Menu

Regex Toolkit API: Test, Explain, and Generate Patterns via API
πŸ“°

Regex Toolkit API: Test, Explain, and Generate Patterns via API

DEV CommunityΒ·2x lazymacΒ·6 months ago
#M11SX9s4
#pattern#api#webdev#tutorial#regex
Reading 0:00
15s threshold

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": [

" [email protected] ",

" [email protected] ",

"not-an-email",

"missing@tld",

"@nodomain.com"

]

})

for result in resp.json()['results']:

print(f"{'βœ…' if result['match'] else '❌'} {result['input']}")

`

Output:

plaintext

βœ… [email protected]

βœ…β€¦

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.