正規表現をテスト表で検証(キャプチャ確認・ReDoS 検出)
Regex test cases: verify a pattern with a match table and ReDoS checks
正規表現を「一致すべき」「一致してはいけない」文字列の表で実行して確かめ、キャプチャグループを表示。破滅的バックトラッキングを起こしやすい書き方を警告し、タイムアウト付きで実測します。
できること
正規表現を「書いたら終わり」にせず、テスト表で動作を確かめてから渡す手順です。
- テキスト形式(
+ 一致すべき文字列/- 一致してはいけない文字列)または JSON 形式のケース表を読み込み、Python のreで実行 - 各ケースの PASS / FAIL、一致した部分、位置、番号付き・名前付きのキャプチャグループを表示。JSON ではグループの期待値も検証可能
(a+)+や(\w+\s?)+のような入れ子の量指定子、重なりのある選択肢の繰り返しなど、破滅的バックトラッキング(ReDoS)を起こしやすい書き方を静的に警告- 各ケースを別プロセスでタイムアウト付き実行するので、危険なパターンでも止まらない。
--stressで入力を伸ばしながら実測し、急激な遅延を確認 - 末尾の改行に一致してしまう
$、全角数字に一致する\dなど、よくある落とし穴の確認手順つき
使い方
python scripts/regex_test.py examples/postal.txt
python scripts/regex_test.py examples/email.json --stress
python scripts/regex_test.py --pattern "(a+)+$" --lint-only
Claude・Codex・Hermes Agent・Cursor に正規表現の作成や修正を頼むと、先にテスト表を作り、すべて通るまで直してから結果と一緒に渡します。
必要な環境
- Python 3.10 以上(標準ライブラリのみ。原子グループ・所有量指定子は 3.11 以上)
注意点
- 検証は Python の
reで行います。JavaScript や Go など他の言語では構文や挙動が異なる部分があり、その注意点も手順に含めています。 - 危険パターンの検出はヒューリスティックです。警告が出ないことは安全の証明ではないため、長い不一致ケースを表に残してください。
- ケースファイルを読むだけで、ファイルの変更や外部への送信はしません。
English description
Run a regex against should-match and should-not-match cases, show captured groups, and flag catastrophic-backtracking patterns with a timed stress test.
What it does
A procedure for never handing over an untested regex.
- Loads a case table in text form (
+ must match/- must not match) or JSON and runs the pattern with Pythonre - Prints PASS/FAIL, the match, span, and numbered and named groups for each case; JSON cases can also assert expected groups
- Statically flags catastrophic-backtracking (ReDoS) constructs such as nested quantifiers like
(a+)+or(\w+\s?)+and repeated overlapping alternatives - Runs every case in a worker process with a timeout so a runaway pattern cannot hang, and
--stresstimes risky fragments on growing input - Includes checks for common traps such as
$matching before a trailing newline and\dmatching full-width digits
How to use
python scripts/regex_test.py examples/postal.txt
python scripts/regex_test.py examples/email.json --stress
python scripts/regex_test.py --pattern "(a+)+$" --lint-only
Ask Claude, Codex, Hermes Agent or Cursor to write or fix a regex; it writes the test table first, iterates until every case passes, and delivers the pattern with the results.
Requirements
- Python 3.10+ (standard library only; atomic groups and possessive quantifiers need 3.11+)
Caveats
- Tests run on Python
re. Other engines such as JavaScript or Go differ in syntax and behavior; porting notes are part of the procedure. - The backtracking lint is heuristic. A clean result is not proof of safety, so keep long non-matching cases in the table.
- It only reads the case file; nothing is modified or sent over the network.
インストール方法
- ダウンロードしたZIPを展開します(中に
regex-test-cases/フォルダとSKILL.mdがあります)。 - フォルダごと、使うエージェントのスキル置き場にコピーします。
- Claude:
~/.claude/skills/(プロジェクト単位なら.claude/skills/) - Hermes Agent:
~/.hermes/skills/ - Codex:
~/.agents/skills//Cursor:~/.cursor/skills/ - Claude アプリ(claude.ai):「カスタマイズ → スキル」からZIPをアップロード
- Claude:
- エージェントを再起動し、スキル一覧に表示されることを確認します。
出品者
出品スキル 44 件