home / skills / ntaksh42 / agents / regex-helper
This skill generates, explains, and tests regular expressions with examples and test cases to simplify pattern creation and debugging.
npx playbooks add skill ntaksh42/agents --skill regex-helperReview the files below or copy the command above to add this skill to your agents.
---
name: regex-helper
description: Generate, explain, and test regular expressions with examples and test cases. Use when creating regex patterns or understanding complex regular expressions.
---
# Regex Helper Skill
正規表現パターンを生成・説明・テストするスキルです。
## 概要
複雑な正規表現を簡単に作成し、既存のパターンを理解しやすく説明します。
## 主な機能
- **パターン生成**: 要件から正規表現を自動生成
- **パターン説明**: 既存の正規表現を人間が読める形で説明
- **テストケース**: マッチする例、しない例を提供
- **最適化**: より効率的なパターンへの変換
- **デバッグ**: 動作しないパターンの修正
- **言語対応**: JavaScript、Python、Java、Go等
## 使用方法
### パターン生成
```
以下の要件で正規表現を生成:
- メールアドレスの検証
- RFC 5322準拠
- サブドメイン対応
```
### パターン説明
```
以下の正規表現を説明:
^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
```
## 生成例
### メールアドレス
**要件**: 基本的なメールアドレス検証
**生成パターン**:
```javascript
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
```
**説明**:
- `^` - 文字列の開始
- `[a-zA-Z0-9._%+-]+` - ユーザー名部分(英数字と一部記号)
- `@` - アットマーク(必須)
- `[a-zA-Z0-9.-]+` - ドメイン名
- `\.` - ドット(エスケープ)
- `[a-zA-Z]{2,}` - トップレベルドメイン(2文字以上)
- `$` - 文字列の終了
**テストケース**:
```
✅ マッチする:
- [email protected]
- [email protected]
- [email protected]
❌ マッチしない:
- invalid.email
- @example.com
- user@
- user@domain
```
### パスワード強度
**要件**:
- 8文字以上
- 大文字1文字以上
- 小文字1文字以上
- 数字1文字以上
- 特殊文字1文字以上
**生成パターン**:
```javascript
/^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/
```
**説明**:
- `(?=.*[A-Z])` - 先読み: 大文字が1つ以上存在
- `(?=.*[a-z])` - 先読み: 小文字が1つ以上存在
- `(?=.*\d)` - 先読み: 数字が1つ以上存在
- `(?=.*[@$!%*?&])` - 先読み: 特殊文字が1つ以上存在
- `[A-Za-z\d@$!%*?&]{8,}` - 許可文字で8文字以上
**テストケース**:
```
✅ マッチする:
- Password123!
- Secure@Pass1
- MyP@ssw0rd
❌ マッチしない:
- password (大文字・数字・記号なし)
- PASSWORD123 (小文字・記号なし)
- Pass123 (7文字・記号なし)
```
### URL抽出
**要件**: テキストからURL を抽出
**生成パターン**:
```python
r'https?://(?:www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_+.~#?&/=]*)'
```
### 日本の電話番号
**要件**: 03-1234-5678 形式
**生成パターン**:
```javascript
/^0\d{1,4}-\d{1,4}-\d{4}$/
```
### クレジットカード番号
**要件**: 16桁、スペース/ハイフン区切り対応
**生成パターン**:
```javascript
/^(?:\d{4}[-\s]?){3}\d{4}$/
```
**テストケース**:
```
✅ マッチする:
- 1234 5678 9012 3456
- 1234-5678-9012-3456
- 1234567890123456
❌ マッチしない:
- 1234 5678 901 (短い)
- abcd-efgh-ijkl-mnop (英字)
```
## パターン説明機能
**入力**:
```
^(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d$
```
**出力**:
```markdown
このパターンは **時刻(HH:MM:SS形式、24時間制)** にマッチします。
構造:
1. `^` - 文字列の開始
2. `(?:[01]\d|2[0-3])` - 時(00-23)
- `[01]\d` - 00-19
- `|` - または
- `2[0-3]` - 20-23
3. `:` - コロン(区切り)
4. `[0-5]\d` - 分(00-59)
5. `:` - コロン(区切り)
6. `[0-5]\d` - 秒(00-59)
7. `$` - 文字列の終了
マッチ例:
✅ 00:00:00
✅ 12:34:56
✅ 23:59:59
マッチしない例:
❌ 24:00:00 (24は無効)
❌ 12:60:00 (60分は無効)
❌ 1:2:3 (0埋めなし)
```
## 最適化機能
**非効率なパターン**:
```
/^[0-9][0-9][0-9][0-9]$/
```
**最適化後**:
```
/^\d{4}$/
```
**改善点**:
- `[0-9]` を `\d` に置換(短く読みやすい)
- 繰り返しを `{4}` で表現
## 言語別パターン
### JavaScript (ECMAScript)
```javascript
const emailRegex = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$/i;
const isValid = emailRegex.test(email);
```
### Python
```python
import re
email_pattern = r'^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$'
is_valid = re.match(email_pattern, email, re.IGNORECASE)
```
### Java
```java
String emailPattern = "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,}$";
Pattern pattern = Pattern.compile(emailPattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(email);
boolean isValid = matcher.matches();
```
## バージョン情報
- スキルバージョン: 1.0.0
- 最終更新: 2025-01-22
---
**使用例**:
```
以下の要件で正規表現を生成:
- 日本の郵便番号(123-4567形式)
- ハイフンあり・なし両対応
- テストケース含む
```
パターン、説明、テストケースが生成されます!
This skill generates, explains, and tests regular expressions for common and complex needs. It helps produce patterns from requirements, breaks down existing regex into readable explanations, and supplies matching and non-matching test cases. Use it to optimize, debug, or adapt patterns for different programming languages.
Provide a requirement or an existing regex and the skill returns a robust pattern, a step-by-step explanation, and example test cases. It can optimize inefficient constructs, suggest language-specific variants (JavaScript, Python, Java, Go), and offer fixes for failing patterns. Outputs include sample matches, non-matches, and implementation snippets for target languages.
Can the skill produce language-specific regex code?
Yes. It provides variants and usage snippets for popular languages such as JavaScript, Python, Java, and Go, adjusting escapes and flags as needed.
Will the generated regex be fully RFC-compliant for protocols like email?
The skill can generate RFC-informed patterns suitable for most practical validation needs, but strict RFC compliance can be extremely permissive; for full protocol conformance consider combining regex checks with dedicated parsers or additional logic.