🛠️ToolsShed

検索・置換

プレーンテキストまたは正規表現でテキストを検索・置換。大文字小文字無視対応。

検索と置換ツールを使用すると、テキスト内のテキストパターンを検索して別のものに置換できます。ブラウザ内ですべて処理され、インストールは不要です。ほとんどのテキストエディタには組み込みの検索/置換機能がありますが、このスタンドアロンツールはこの機能がない環境での作業や、特定の置換操作を共有または文書化する場合に便利です。

検索語と置換テキストを入力し、「すべて置換」をクリックしてすべての一致を一度に置換するか、「次を置換」を使用して一致を一つずつ処理します。大文字小文字の区別/無視、完全一致、正規表現(regex)モードをサポートします。

正規表現は、正確なテキストではなくパターンに従うコンテンツを検索・置換するための強力な方法です。例えば、YYYY-MM-DD形式のすべての日付を見つけて一度の操作でDD/MM/YYYYに再フォーマットできます。ここでサポートされる正規表現構文は標準のJavaScript正規表現エンジンに従います。

よくある質問

コード実装

import re

def find_replace(text: str, find: str, replace: str,
                 use_regex: bool = False, case_sensitive: bool = True) -> str:
    """Find and replace text, with optional regex and case-insensitive modes."""
    if use_regex:
        flags = 0 if case_sensitive else re.IGNORECASE
        return re.sub(find, replace, text, flags=flags)
    else:
        if case_sensitive:
            return text.replace(find, replace)
        else:
            # Case-insensitive plain string replace
            return re.sub(re.escape(find), replace, text, flags=re.IGNORECASE)

# Examples
text = "Hello World. Hello Python."
print(find_replace(text, "Hello", "Hi"))
# Hi World. Hi Python.

print(find_replace(text, r"\bHello\b", "Greetings", use_regex=True))
# Greetings World. Greetings Python.

print(find_replace(text, "hello", "Hi", case_sensitive=False))
# Hi World. Hi Python.

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.