You're viewing version 1.2.0 of re-pattern. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

re-pattern

clojure.core

  • (re-pattern s)
Returns an instance of java.util.regex.Pattern, for use, e.g. in
re-matcher.

1 Example top

  • user=> (re-pattern "\\d+")
    #"\d+"
    
    user=> (re-find (re-pattern "\\d+") "abc123def") 
    "123"
    
    ;; If you want to construct a regex pattern dynamically at run time,
    ;; then you need to use re-pattern to convert a string to a pattern
    ;; that can be used for matching.  But if your pattern is one you
    ;; write into the source code, it is more convenient to use the
    ;; #"pattern" syntax.  The previous example can be written as follows.
    user=> (re-find #"\d+" "abc123def") 
    "123"
    
    ;; Below are two examples that are equivalent in the patterns they
    ;; use, but the #"pattern" syntax helps significantly, because it lets
    ;; us avoid the requirement to escape every \ character with another \
    ;; character.  See the example with embedded comments below for more
    ;; detail on what the pattern matches.
    user=> (re-find #"\\\d+\s+\S+" "\\ it sh0uld match in \\5 here somewhere.")
    "\\5 here"
    
    user=> (re-find (re-pattern "\\\\\\d+\\s+\\S+")
                    "\\ it sh0uld match in \\5 here somewhere.")
    "\\5 here"
    
    ;; If you want to embed (ignored) whitespace and comments from #
    ;; characters until end-of-line in your regex patterns, start the
    ;; pattern with (?x)
    user=> (re-find #"(?x)  # allow embedded whitespace and comments
                      \\    # backslash
                      \d+   # one or more digits
                      \s+   # whitespace
                      \S+   # non-whitespace"
                    "\\ it sh0uld match in \\5 here somewhere.")
    "\\5 here"
    
    ;; Other pattern flags like Java's DOTALL, MULTILINE and UNICODE_CASE
    ;; pattern matching modes, can be set by combining these embedded flags
    
    ;; (?d) Unix lines (only match \newline)
    ;; (?i) Case-insensitive
    ;; (?u) Unicode-aware Case
    ;; (?m) Multiline
    ;; (?s) Dot matches all (including newline)
    ;; (?x) Ignore Whitespace and comments
    
    user=> (re-seq #"(?ix) test #Case insensitive and comments allowed"
                   "Testing,\n testing,\n 1 2 3")
    ("Test" "test")
    
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:3745 top

(defn re-pattern
  "Returns an instance of java.util.regex.Pattern, for use, e.g. in
  re-matcher."
  {:tag java.util.regex.Pattern
   :added "1.0"}
  [s] (if (instance? java.util.regex.Pattern s)
        s
        (. java.util.regex.Pattern (compile s))))
Vars in clojure.core/re-pattern: compile defn instance?
Used in 1 other vars get-by-pattern

Comments top

No comments for re-pattern. Log in to add a comment.