Clojure-regular-expressions-replace

提供:Dev Guides
移動先:案内検索

Clojure-正規表現に置き換わる

交換する

replace関数は、ストリング内のサブストリングを新しいストリング値で置き換えるために使用されます。 部分文字列の検索は、パターンを使用して行われます。

構文

構文は次のとおりです。

(replace str pat replacestr)

パラメータ-「pat」は正規表現パターンです。 「str」は、パターンに基づいてテキストを検索する必要がある文字列です。 「replacestr」は、パターンに基づいて元の文字列で置き換える必要がある文字列です。

戻り値-部分文字列の置換が正規表現パターンを介して行われる新しい文字列。

Clojureでの置き換えの例を次に示します。

(ns clojure.examples.example
   (:gen-class))

;; This program displays Hello World
(defn Example []
   (def pat (re-pattern "\\d+"))
   (def newstr (clojure.string/replace "abc123de" pat "789"))
   (println newstr))
(Example)

出力

上記のプログラムは、次の出力を生成します。

abc789de