home / skills / ntaksh42 / agents / regex-helper

regex-helper skill

/.claude/skills/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-helper

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
5.0 KB
---
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形式)
- ハイフンあり・なし両対応
- テストケース含む
```

パターン、説明、テストケースが生成されます!

Overview

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.

How this skill works

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.

When to use it

  • When you need a reliable regex from plain-language requirements
  • When you must understand or document an unfamiliar complex regex
  • When testing patterns with representative match/non-match examples
  • When optimizing performance or simplifying a verbose pattern
  • When porting a regex between programming languages

Best practices

  • Describe validation requirements precisely (allowed characters, length, anchors)
  • Prefer explicit character classes and quantified repetitions over repeated single-character groups
  • Add anchors (^ and $) when validating whole strings; use word boundaries for token extraction
  • Provide positive and negative test cases to verify edge conditions
  • Use non-capturing groups (?: ) when grouping is needed but captures are not

Example use cases

  • Generate an RFC-like email validator and language-specific implementations
  • Explain a password-strength regex and show failing examples to guide users
  • Produce URL extraction patterns and show usage in a text parser
  • Convert a verbose digit-by-digit pattern into a compact \d{n} form and explain the change
  • Create phone number, postal code, credit card, and time-format validators with test cases

FAQ

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.