🔤 Regex Builder Skill
name: regex-builder
description: Build and explain regular expressions from natural language descriptions
🎯 Purpose
สร้าง Regular Expressions จาก natural language และอธิบาย regex ที่ซับซ้อนให้เข้าใจง่าย
📋 When to Use
- ต้องการสร้าง regex ใหม่
- ต้องการเข้าใจ regex ที่มีอยู่
- Validate input patterns
- Extract data จาก text
- Search and replace patterns
🔧 Common Patterns
Email Validation
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$
Explanation:
^ - เริ่มต้น string
[a-zA-Z0-9._%+-]+ - username (ตัวอักษร, ตัวเลข, ., _, %, +, -)
@ - ต้องมี @
[a-zA-Z0-9.-]+ - domain name
\. - ต้องมี .
[a-zA-Z]{2,}$ - TLD อย่างน้อย 2 ตัวอักษร
Phone Number (Thai)
^(0[689]\d{8}|0[2-9]\d{7,8})$
Explanation:
0[689]\d{8} - มือถือ (06, 08, 09 + 8 หลัก)
0[2-9]\d{7,8} - บ้าน (02-09 + 7-8 หลัก)
URL
^https?:\/\/[^\s]+$
Date (YYYY-MM-DD)
^\d{4}-\d{2}-\d{2}$
Password (Strong)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
Explanation:
(?=.*[a-z]) - ต้องมี lowercase
(?=.*[A-Z]) - ต้องมี uppercase
(?=.*\d) - ต้องมีตัวเลข
(?=.*[@$!%*?&]) - ต้องมี special char
{8,} - อย่างน้อย 8 ตัว
📊 Regex Cheat Sheet
Character Classes
| Pattern |
Meaning |
. |
Any character (except newline) |
\d |
Digit (0-9) |
\D |
Non-digit |
\w |
Word character (a-z, A-Z, 0-9, _) |
\W |
Non-word character |
\s |
Whitespace |
\S |
Non-whitespace |
Quantifiers
| Pattern |
Meaning |
* |
0 or more |
+ |
1 or more |
? |
0 or 1 |
{n} |
Exactly n |
{n,} |
n or more |
{n,m} |
Between n and m |
Anchors
| Pattern |
Meaning |
^ |
Start of string |
$ |
End of string |
\b |
Word boundary |
Groups
| Pattern |
Meaning |
(...) |
Capture group |
(?:...) |
Non-capture group |
(?=...) |
Positive lookahead |
(?!...) |
Negative lookahead |
📝 Natural Language → Regex
| Natural Language |
Regex |
| "starts with A" |
^A |
| "ends with .com" |
\.com$ |
| "contains numbers" |
\d+ |
| "exactly 5 digits" |
^\d{5}$ |
| "optional dashes" |
-? |
| "one or more words" |
\w+ |
| "either cat or dog" |
(cat|dog) |
🔧 JavaScript Usage
// Test pattern
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
emailRegex.test('user@example.com'); // true
// Extract matches
const text = 'Call 081-234-5678 or 089-876-5432';
const phones = text.match(/0[689]\d-?\d{3}-?\d{4}/g);
// ['081-234-5678', '089-876-5432']
// Replace
const cleaned = text.replace(/[^\d]/g, '');
// '08123456780898765432'
// Named groups
const dateRegex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const match = '2024-01-15'.match(dateRegex);
console.log(match.groups.year); // '2024'
🔍 Regex Debugging
Test Online
Common Mistakes
| Mistake |
Fix |
Forgot to escape . |
Use \. |
| Greedy matching |
Use *? or +? |
| Missing anchors |
Add ^ and $ |
| Global flag missing |
Add g flag |
✅ Regex Checklist
🔗 Related Skills
code-search - Search patterns in code
data-analysis - Extract data from text
testing - Test regex patterns
1---2name: regex-builder3description: 🔤 Regex Builder Skill4---5# 🔤 Regex Builder Skill67---8name: regex-builder9description: Build and explain regular expressions from natural language descriptions10---1112## 🎯 Purpose1314สร้าง Regular Expressions จาก natural language และอธิบาย regex ที่ซับซ้อนให้เข้าใจง่าย1516## 📋 When to Use1718- ต้องการสร้าง regex ใหม่19- ต้องการเข้าใจ regex ที่มีอยู่20- Validate input patterns21- Extract data จาก text22- Search and replace patterns2324## 🔧 Common Patterns2526### Email Validation27```regex28^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$29```30**Explanation:**31- `^` - เริ่มต้น string32- `[a-zA-Z0-9._%+-]+` - username (ตัวอักษร, ตัวเลข, ., _, %, +, -)33- `@` - ต้องมี @34- `[a-zA-Z0-9.-]+` - domain name35- `\.` - ต้องมี .36- `[a-zA-Z]{2,}$` - TLD อย่างน้อย 2 ตัวอักษร3738### Phone Number (Thai)39```regex40^(0[689]\d{8}|0[2-9]\d{7,8})$41```42**Explanation:**43- `0[689]\d{8}` - มือถือ (06, 08, 09 + 8 หลัก)44- `0[2-9]\d{7,8}` - บ้าน (02-09 + 7-8 หลัก)4546### URL47```regex48^https?:\/\/[^\s]+$49```5051### Date (YYYY-MM-DD)52```regex53^\d{4}-\d{2}-\d{2}$54```5556### Password (Strong)57```regex58^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$59```60**Explanation:**61- `(?=.*[a-z])` - ต้องมี lowercase62- `(?=.*[A-Z])` - ต้องมี uppercase63- `(?=.*\d)` - ต้องมีตัวเลข64- `(?=.*[@$!%*?&])` - ต้องมี special char65- `{8,}` - อย่างน้อย 8 ตัว6667## 📊 Regex Cheat Sheet6869### Character Classes70| Pattern | Meaning |71|---------|---------|72| `.` | Any character (except newline) |73| `\d` | Digit (0-9) |74| `\D` | Non-digit |75| `\w` | Word character (a-z, A-Z, 0-9, _) |76| `\W` | Non-word character |77| `\s` | Whitespace |78| `\S` | Non-whitespace |7980### Quantifiers81| Pattern | Meaning |82|---------|---------|83| `*` | 0 or more |84| `+` | 1 or more |85| `?` | 0 or 1 |86| `{n}` | Exactly n |87| `{n,}` | n or more |88| `{n,m}` | Between n and m |8990### Anchors91| Pattern | Meaning |92|---------|---------|93| `^` | Start of string |94| `$` | End of string |95| `\b` | Word boundary |9697### Groups98| Pattern | Meaning |99|---------|---------|100| `(...)` | Capture group |101| `(?:...)` | Non-capture group |102| `(?=...)` | Positive lookahead |103| `(?!...)` | Negative lookahead |104105## 📝 Natural Language → Regex106107| Natural Language | Regex |108|-----------------|-------|109| "starts with A" | `^A` |110| "ends with .com" | `\.com$` |111| "contains numbers" | `\d+` |112| "exactly 5 digits" | `^\d{5}$` |113| "optional dashes" | `-?` |114| "one or more words" | `\w+` |115| "either cat or dog" | `(cat\|dog)` |116117## 🔧 JavaScript Usage118119```javascript120// Test pattern121const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;122emailRegex.test('user@example.com'); // true123124// Extract matches125const text = 'Call 081-234-5678 or 089-876-5432';126const phones = text.match(/0[689]\d-?\d{3}-?\d{4}/g);127// ['081-234-5678', '089-876-5432']128129// Replace130const cleaned = text.replace(/[^\d]/g, '');131// '08123456780898765432'132133// Named groups134const dateRegex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;135const match = '2024-01-15'.match(dateRegex);136console.log(match.groups.year); // '2024'137```138139## 🔍 Regex Debugging140141### Test Online142- [regex101.com](https://regex101.com) - With explanation143- [regexr.com](https://regexr.com) - Visual144145### Common Mistakes146| Mistake | Fix |147|---------|-----|148| Forgot to escape `.` | Use `\.` |149| Greedy matching | Use `*?` or `+?` |150| Missing anchors | Add `^` and `$` |151| Global flag missing | Add `g` flag |152153## ✅ Regex Checklist154155- [ ] Handles edge cases156- [ ] Not too greedy157- [ ] Anchored properly158- [ ] Tested with valid inputs159- [ ] Tested with invalid inputs160- [ ] Performance acceptable161162## 🔗 Related Skills163164- `code-search` - Search patterns in code165- `data-analysis` - Extract data from text166- `testing` - Test regex patterns