コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
re.replace
re.replace(stringText, replaceRegex, replacementText)
説明
正規表現の置き換えを行います。
この関数は次の 3 つの引数を取ります。
stringText
: 元の文字列。
replaceRegex
: 検索するパターンを示す正規表現。
replacementText
: 各一致に挿入するテキスト。
元の文字列 stringText
から派生した新しい文字列を返します。ここでは、replaceRegex
のパターンに一致するすべての部分文字列が、replacementText
に置き換えられます。replacementText
内でバックスラッシュでエスケープされた数字(\1
~\9
)を使用して、対応する括弧で囲まれたグループと一致するテキストを replaceRegex
パターン内に挿入できます。一致するテキスト全体を参照するには、\0
を使用します。
この関数は、重複しない一致を置き換えるもので、最初に見つかったものを優先的に置き換えます。たとえば、re.replace("banana", "ana", "111")
は文字列「b111na」を返します。
パラメータのデータ型
STRING
, STRING
, STRING
戻り値の型
STRING
コードサンプル
例 1
この例では、メール内の @
記号の後のすべての部分をキャプチャし、com
を org
に置き換えて結果を返します。ネストされた関数が使用されることに注意してください。
"email@google.org" = re.replace($e.network.email.from, "com", "org")
例 2
次の例では、replacementText
引数でバックスラッシュでエスケープされた数字を使用して、replaceRegex
パターンとの一致を参照します。
"test1.com.google" = re.replace(
$e.principal.hostname, // holds "test1.test2.google.com"
"test2\.([a-z]*)\.([a-z]*)",
"\\2.\\1" // \\1 holds "google", \\2 holds "com"
)
例 3
空の文字列と re.replace()
を扱う場合は、次の点に注意してください。
replaceRegex
として空の文字列を使用。
// In the function call below, if $e.principal.hostname contains "name",
// the result is: 1n1a1m1e1, because an empty string is found next to
// every character in `stringText`.
re.replace($e.principal.hostname, "", "1")
空の文字列を置き換えるには、replaceRegex
として "^$"
を使用します。
// In the function call below, if $e.principal.hostname contains the empty
// string, "", the result is: "none".
re.replace($e.principal.hostname, "^$", "none")
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-29 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-29 UTC。"],[[["\u003cp\u003e\u003ccode\u003ere.replace\u003c/code\u003e is a function that performs regular expression replacement within a string.\u003c/p\u003e\n"],["\u003cp\u003eIt requires three string arguments: the original string, the regex pattern to match, and the replacement text.\u003c/p\u003e\n"],["\u003cp\u003eBackslash-escaped digits (\u003ccode\u003e\\1\u003c/code\u003e to \u003ccode\u003e\\9\u003c/code\u003e) can be used in the replacement text to insert text from captured groups in the regex pattern, with \u003ccode\u003e\\0\u003c/code\u003e representing the entire match.\u003c/p\u003e\n"],["\u003cp\u003eThe function prioritizes the first non-overlapping match found and replaces it, as seen in the "banana" example being changed to "b111na".\u003c/p\u003e\n"],["\u003cp\u003eUsing an empty string as the \u003ccode\u003ereplaceRegex\u003c/code\u003e will match next to every character, while \u003ccode\u003e"^$"\u003c/code\u003e is used to target and replace an empty string.\u003c/p\u003e\n"]]],[],null,["### re.replace\n\nSupported in: \n[Rules](/chronicle/docs/detection/default-rules) [Search](/chronicle/docs/investigation/udm-search) \n\n re.replace(stringText, replaceRegex, replacementText)\n\n#### Description\n\nPerforms a regular expression replacement.\n\nThis function takes three arguments:\n\n- `stringText`: the original string.\n- `replaceRegex`: the regular expression indicating the pattern to search for.\n- `replacementText`: The text to insert into each match.\n\nReturns a new string derived from the original `stringText`, where all\nsubstrings that match the pattern in `replaceRegex` are replaced with the value in\n`replacementText`. You can use backslash-escaped digits (`\\1` to `\\9`) within\n`replacementText` to insert text matching the corresponding parenthesized group\nin the `replaceRegex` pattern. Use `\\0` to refer to the entire matching text.\n\nThe function replaces non-overlapping matches and will prioritize replacing the\nfirst occurrence found. For example, `re.replace(\"banana\", \"ana\", \"111\")`\nreturns the string \"b111na\".\n\n#### Param data types\n\n`STRING`, `STRING`, `STRING`\n\n#### Return type\n\n`STRING`\n\n#### Code samples\n\n##### Example 1\n\nThis example captures everything after the `@` symbol in an email, replaces `com`\nwith `org`, and then returns the result. Notice the use of nested functions. \n\n \"email@google.org\" = re.replace($e.network.email.from, \"com\", \"org\")\n\n##### Example 2\n\nThis example uses backslash-escaped digits in the `replacementText` argument to\nreference matches to the `replaceRegex` pattern. \n\n \"test1.com.google\" = re.replace(\n $e.principal.hostname, // holds \"test1.test2.google.com\"\n \"test2\\.([a-z]*)\\.([a-z]*)\",\n \"\\\\2.\\\\1\" // \\\\1 holds \"google\", \\\\2 holds \"com\"\n )\n\n##### Example 3\n\nNote the following cases when dealing with empty strings and `re.replace()`:\n\nUsing empty string as `replaceRegex`: \n\n // In the function call below, if $e.principal.hostname contains \"name\",\n // the result is: 1n1a1m1e1, because an empty string is found next to\n // every character in `stringText`.\n re.replace($e.principal.hostname, \"\", \"1\")\n\nTo replace an empty string, you can use `\"^$\"` as `replaceRegex`: \n\n // In the function call below, if $e.principal.hostname contains the empty\n // string, \"\", the result is: \"none\".\n re.replace($e.principal.hostname, \"^$\", \"none\")"]]