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

この例では、メール内の @ 記号の後のすべての部分をキャプチャし、comorg に置き換えて結果を返します。ネストされた関数が使用されることに注意してください。

"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")