MeWrite Docs

Invalid regular expression

正規表現のパターンが不正な場合のエラー

概要

正規表現のパターンに構文エラーがある場合に発生するエラーです。エスケープの問題、括弧の不一致、無効な量指定子などが原因です。

エラーメッセージ

SyntaxError: Invalid regular expression: /pattern/: message
SyntaxError: Invalid regular expression: Unterminated group
SyntaxError: Invalid regular expression: Nothing to repeat
re.error: bad escape \x at position 5 (Python)

原因

  1. エスケープ漏れ: 特殊文字をエスケープしていない
  2. 括弧の不一致: ()の数が合わない
  3. 無効な量指定子: *+の前に何もない
  4. 無効な文字クラス: []の中身が不正
  5. 言語・エンジンの違い: 正規表現の方言

解決策

1. 特殊文字のエスケープ

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Bad: 特殊文字をエスケープしていない
const regex = /file.txt/;  // . は任意の1文字

// Good: エスケープする
const regex = /file\.txt/;

// エスケープが必要な特殊文字
// . * + ? ^ $ { } [ ] ( ) | \ /

// 動的に作成する場合
const filename = 'file.txt';
const escaped = filename.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const regex = new RegExp(escaped);

2. 括弧の一致

1
2
3
4
5
6
7
8
// Bad: 括弧が閉じていない
const regex = /((abc)/;  // SyntaxError

// Good: 括弧を閉じる
const regex = /((abc))/;

// グループの確認
// ( を数える = ) を数える

3. 量指定子の正しい使用

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Bad: 量指定子の前に何もない
const regex = /*abc/;    // SyntaxError
const regex = /+abc/;    // SyntaxError
const regex = /?abc/;    // SyntaxError

// Good: 量指定子の前に文字や文字クラス
const regex = /a*bc/;    // 0回以上のa
const regex = /a+bc/;    // 1回以上のa
const regex = /a?bc/;    // 0か1回のa
const regex = /.+abc/;   // 任意の1文字以上

4. 文字クラス

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Bad: 文字クラスが閉じていない
const regex = /[abc/;    // SyntaxError

// Good: 文字クラスを閉じる
const regex = /[abc]/;

// 文字クラス内での特殊文字
const regex = /[a-z]/;   // 範囲
const regex = /[\-]/;    // ハイフン(エスケープまたは先頭/末尾)
const regex = /[abc-]/;  // ハイフンを末尾に
const regex = /[\]]/;    // 閉じ括弧をエスケープ
const regex = /[[\]]/;   // [] のどちらか

5. 後方参照

1
2
3
4
5
6
7
8
// Bad: 存在しないグループを参照
const regex = /\1abc/;   // グループがない

// Good: グループを先に定義
const regex = /(abc)\1/;  // abcabc にマッチ

// 名前付きグループ(ES2018+)
const regex = /(?<word>\w+)\s+\k<word>/;  // 同じ単語の繰り返し

6. 先読み・後読み

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// 肯定先読み
const regex = /foo(?=bar)/;   // foobar の foo にマッチ

// 否定先読み
const regex = /foo(?!bar)/;   // foobar 以外の foo にマッチ

// 肯定後読み(ES2018+)
const regex = /(?<=\$)\d+/;   // $100 の 100 にマッチ

// 否定後読み(ES2018+)
const regex = /(?<!\$)\d+/;   // $ の後ろ以外の数字

7. フラグの使用

1
2
3
4
5
6
7
8
9
// フラグ
const regex = /pattern/gi;    // g: グローバル, i: 大文字小文字無視
const regex = /pattern/m;     // m: 複数行モード
const regex = /pattern/s;     // s: . が改行にもマッチ(ES2018+)
const regex = /pattern/u;     // u: Unicode
const regex = /pattern/y;     // y: スティッキー

// new RegExp での指定
const regex = new RegExp('pattern', 'gi');

8. Pythonでの正規表現

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
import re

# 生の文字列を使用(推奨)
pattern = r'\d+\.\d+'

# Bad: エスケープ問題
pattern = '\d+'  # 警告が出る

# コンパイル
regex = re.compile(r'\d+')
result = regex.findall('abc123def456')

# マッチ
match = re.match(r'(\d+)', '123abc')
if match:
    print(match.group(1))

9. よくあるパターン

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
// メールアドレス(簡易版)
const email = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

// URL
const url = /https?:\/\/[^\s]+/;

// 電話番号(日本)
const phone = /^0\d{1,4}-\d{1,4}-\d{4}$/;

// IPアドレス
const ip = /^(\d{1,3}\.){3}\d{1,3}$/;

// 日付(YYYY-MM-DD)
const date = /^\d{4}-\d{2}-\d{2}$/;

10. 正規表現のテスト

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// 基本的なテスト
const regex = /pattern/;
console.log(regex.test('some pattern here'));  // true

// マッチ結果
const result = 'hello world'.match(/(\w+)/g);
console.log(result);  // ['hello', 'world']

// 置換
const result = 'hello world'.replace(/world/, 'there');
console.log(result);  // 'hello there'

11. 安全な正規表現の作成

1
2
3
4
5
6
7
8
// ユーザー入力から正規表現を作成する場合
function escapeRegExp(string) {
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

const userInput = 'file.txt';
const safePattern = escapeRegExp(userInput);
const regex = new RegExp(safePattern);

主な正規表現エンジンの違い

機能JavaScriptPythonPCRE
後読みES2018+
名前付きグループ(?<name>)(?P<name>)(?P<name>)
Unicode/u フラグデフォルト設定による

よくある間違い

  • ユーザー入力をエスケープせずに正規表現に使用
  • *+の違いを理解していない
  • 貪欲マッチと非貪欲マッチ(*?)の違い
  • 文字列リテラルでのバックスラッシュのエスケープ忘れ

関連エラー

参考リンク

正規表現 の他のエラー

最終更新: 2025-12-13