regex

Parsing

高度な正規表現エンジン

概要 regex

regex モジュールは、Python の組み込み re モジュールのドロップイン代替品であり、ファジー マッチング (近似文字列マッチング)、可変幅後読み、繰り返しキャプチャを行う名前付きキャプチャ グループ、POSIX 文字クラス、アトミック グループ化などの追加機能を提供します。

正規表現は、micropip 経由でPyRunで利用できます。これは、わずかなタイプミスのあるパターンの一致 (あいまい一致)、複雑な Unicode テキストの操作、または標準の re モジュールでサポートされていない正規表現機能の使用が必要な場合に特に便利です。

コードと実行の出力

名前付きキャプチャ グループとファジー パターン マッチング。

Regex: Named Groups & Fuzzyエディターで実行
import regex

# Named capture groups — parse a log line
log = "2025-07-04 14:32:01 ERROR [auth] Login failed for user@example.com"
pattern = r"(?P<date>\d{4}-\d{2}-\d{2}) (?P<time>\d{2}:\d{2}:\d{2}) (?P<level>\w+) \[(?P<module>\w+)\] (?P<message>.+)"
m = regex.match(pattern, log)
if m:
    for k, v in m.groupdict().items():
        print(f"  {k:<10}: {v}")

# Fuzzy matching — find 'colour' with up to 1 error
print("\nFuzzy search (≤1 substitution):")
text = "I prefer colour and cilor and colouur"
for hit in regex.finditer(r"(?:colour){s<=1}", text):
    print(f"  found '{hit.group()}' at {hit.span()}")

関連パッケージ

推奨される Python リソース

関連するインタラクティブなチュートリアル、チートシート、コード比較で知識を深めてください。