Clojure - 正則表示式替換



替換

replace 函式用於將字串中的子字串替換為新的字串值。子字串的搜尋是使用模式進行的。

語法

以下是語法。

(replace str pat replacestr)

引數 - ‘pat’ 是正則表示式模式。‘str’ 是需要根據模式查詢文字的字串。‘replacestr’ 是需要根據模式替換原始字串中的字串。

返回值 - 新字串,其中透過正則表示式模式對子字串進行了替換。

示例

以下是 Clojure 中 replace 的示例。

(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
clojure_regular_expressions.htm
廣告

© . All rights reserved.