- Home
- Skills
- Prompt & Skill Engineering
- pattern grammar regex replacement
pattern grammar regex replacement
Replace unreadable regex with maintainable, BNF-style grammar rules for Python validation and parsing.
Free
Works with the AI Agents you already use
pattern grammar regex replacement
Example session with this skill installed
Create a pattern grammar for a simple ISO 8601 date (YYYY-MM-DD) with custom error messages for each component.
- Read your context and instructions
- Compiled the pattern grammar regex
The grammar below decomposes the ISO 8601 date format into named rules for year, month, and day, each with specific @fail messages for granular error reporting.
ISO 8601 Date Grammar
# Top-level rule
date ::= year "-" month "-" day @fail "Invalid date format, expected YYYY-MM-DD"
# Component rules
year ::= [0-9]{4}
@fail "Year must be exactly 4 digits"
month ::= "0" [1-9] | "1" [0-2]
@fail "Month must be between 01 and 12"
day ::= "0" [1-9] | [12] [0-9] | "3" [01]
@fail "Day must be between 01 and 31"
Next steps
- Save this grammar to a file named
date_format.pgor load it directly as a string into thePatternconstructor. - Use
pattern.match('date', input_string)within a try-except block to catchPatternErrorand surface the custom messages. - Add a
timerule and an optional separator to extend this into a full ISO 8601 datetime validator.
Connects securely to your tools. The creator never sees your data.
About this skill
Opaque regular expressions are a liability in production code. They are difficult to read, impossible to unit test in isolation, and fail silently on complex edge cases like nested structures or IPv6 addresses. This skill replaces monolithic regex strings with human-readable, BNF-like pattern grammar rules that are easy to maintain and document.
What it does
- Decompose regex by breaking complex expressions into named, logical components.
- Auto-select engine to use standard regex for flat patterns and Lark LALR parsers for recursive, nested data.
- Validate formats for common standards including URL (RFC 3986), email, ISO 8601, SemVer, and JSON.
- Custom error reporting using
@faildirectives to return specific, actionable messages when a sub-rule fails. - Generate test data by producing random strings that conform to your defined grammar rules.
- Pydantic integration to enforce string formats directly within data models using custom validators.
How it works
- Define rules using
::=syntax to describe each part of your format, from character classes to top-level assembly. - Import grammars from local
.pgfiles or built-in libraries to reuse standard patterns likerfc3986-core. - Initialize patterns in Python to compile the grammar into its most efficient execution form.
- Execute checks using
match()for Boolean validation orparse()to retrieve a structured AST tree.
Frameworks & tools
This tool uses the pattern-grammar Python library, which depends on lark. It optionally integrates with pydantic for schema validation and the regex package for advanced Unicode character class support.
Why this beats prompting it yourself
LLMs often hallucinate complex regex lookahead logic or miss subtle RFC requirements. This skill provides a systematic framework for building verifiable grammars that handle recursion and white-space correctly, ensuring the resulting code is actually maintainable by humans.
Use cases
- Replace a 40-line URL regex with a structured, multi-line RFC-compliant grammar.
- Validate nested math or logic expressions that standard regex cannot parse.
- Build Pydantic models that return specific "Invalid TLD" errors instead of generic failures.
- Generate synthetic data for testing form validation logic.
Known limitations
Does not support lookahead or lookbehind. Rules must be defined via structure or handled in post-parse code. Whitespace is only auto-ignored in recursive rules. Quote escaping is not supported.
Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for Prompt & Skill Engineering.
No lookahead/lookbehind support. Whitespace is literal in regex rules. Quote escaping via backslash is unsupported. Recursive depth capped at 100 levels. findall() only works on non-recursive rules.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before buying a skill.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Free to use
No account required to browse
Trust & safety
Security scan
Security scan passed 1 day ago
- Free with an Agensi account
- Software
Python
Git