A skill for using Copilot to build, explain, and test regular expressions with natural language descriptions and validation.
## Copilot Regex Builder Skill ### Overview Use Copilot to generate correct regular expressions from natural language descriptions, with explanations and test cases. ### Method 1: Comment-Driven Regex Describe what you want to match in a comment: ```javascript // Match email addresses: [email protected] const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; // Match US phone numbers: (123) 456-7890 or 123-456-7890 or 1234567890 const phoneRegex = /^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/; // Match ISO 8601 dates: 2024-01-15 or 2024-01-15T10:30:00Z const isoDateRegex = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})?)?$/; ``` ### Method 2: Chat-Based Regex Generation Ask Copilot Chat for complex patterns: ``` Create a regex that matches: - URLs with http or https protocol - Optional www subdomain - Domain with 2-63 character labels - Optional port number (1-65535) - Optional path, query string, and fragment - Should NOT match bare domains without protocol Provide the regex with a breakdown of each part. ``` ### Method 3: Regex Explanation Paste an existing regex and ask Copilot to explain it: ``` Explain this regex step by step: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/ Copilot explains: - ^: start of string - (?=.*[a-z]): positive lookahead for lowercase letter - (?=.*[A-Z]): positive lookahead for uppercase letter - (?=.*\d): positive lookahead for digit - (?=.*[@$!%*?&]): positive lookahead for special character - [A-Za-z\d@$!%*?&]{8,}: 8 or more of the allowed characters - $: end of string ``` ### Method 4: Test Case Generation Ask Copilot to generate test cases for your regex: ``` Generate test cases for this email validation regex. Include: valid emails, invalid emails, edge cases. Format as a test array with { input, expected } objects. ``` ### Common Regex Patterns **Validation:** - Email, phone, URL, IP address (v4 and v6) - Date formats (ISO, US, EU), time, datetime - Credit card numbers (Visa, Mastercard, Amex) - Strong password requirements **Extraction:** - Extract URLs from text - Parse CSV with quoted fields containing commas - Extract hashtags and mentions from social media text - Parse log file entries with timestamp, level, and message **Replacement:** - Sanitize HTML tags from user input - Convert Markdown links to HTML anchor tags - Normalize whitespace (multiple spaces to single) - Mask sensitive data (email, phone, credit card) ### Tips - Always test regex with edge cases before deploying - Use named capture groups for readability: `(?<year>\d{4})` - Prefer specific character classes over `.` to avoid over-matching - Use non-greedy quantifiers `*?` and `+?` when appropriate - Consider using a regex library for complex validations instead of raw regex
Free to copy and use. Compatible with GitHub Copilot, GPT-5.
Write a descriptive comment above where you need a regex, and Copilot will generate it. Use Copilot Chat for complex patterns that need explanation and test cases.
Initial release
gh copilot skill install copilot-regex-builder-pattern-skillSign in and download this prompt to leave a review.