Regular expressions in general are character sequence or code formats used in search patterns to streamline the search results of a string. A regular expression is used in the string search algorithm.
replace
method?The replace
method is used to replace the match pattern with a substring in a parent string.
(replace string pattern replacestr)
The replace
method receives one parameter following the syntax above.
pattern
: This is the pattern to be built.string
: This is the parent string that contains the substring to be matched.replacestr
: This is the string we replace in the parent string after matching the pattern.The replace
method returns a string with the replaced string.
(ns clojure.examples.example(:gen-class))(defn func [](def pattern (re-pattern "\\d+"))(def newstring (clojure.string/replace "sam409uel" pattern "993"))(println newstring))(func)
func.
re-pattern
method (re-pattern "\\d+")
, and we save it to a variable pattern
.clojure.string/replace
. We basically match all numbers in the string with sam409uel
and replace them with 993
. newstring
to the console.func
.