åæ¥ã¨ã´ã¼ã«ãã³ã¦ã£ã¼ã¯ãä»äºã°ã£ããã§ãå ¨ç¶æ´æ°ã§ãã¦ãã¾ããã§ãããâ¦ããªãã¨ãæéãä½ããã®ã§ãä¹ ã ã®æ´æ°ã§ãã
ååã®Scalaã®æ£è¦è¡¨ç¾ã«ç¶ããClojureã®æ£è¦è¡¨ç¾ã使ã£ã¦ã¿ããã¨æãã¾ããContribã¯è¦ãã«ãæ¨æºé¢æ°ã®ã¿ã§ãã
re-patternï¼java.util.regex.Patternã®æ§ç¯ï¼
re-patterné¢æ°ã«ãæ£è¦è¡¨ç¾ã表ãæååã渡ãã¾ãã
(def number-pattern (re-pattern "\\d+")) (println (class number-pattern)) ;; => java.util.regex.Pattern
ãããã¯ã#ãªã¼ãã¼ãã¯ãã§ãOKã§ãã
(def number-pattern #"\d+") (println (class #"\d+")) ;; => java.util.regex.Pattern
ãã®å ´åãJavaã§æ£è¦è¡¨ç¾ãè¨è¿°ããéã«é¢åãªãããã¯ã¹ã©ãã·ã¥ã®ã¨ã¹ã±ã¼ãã楽ã«ãªãã¾ãã
ã§ã¯ããã®è¾ºããã¹ã±ã¼ãã´ã¼ãã«ãã¦ã試ãã¦ãã£ã¦ã¿ã¾ãããã
(def target-str "Hello 1234 World 5678") (def number-pattern (re-pattern "\\d+")) (def alpha-pattern (re-pattern "[a-zA-Z]+")) (def not-match-pattern (re-pattern ":"))
re-matcherï¼java.util.regex.Matcherã®æ§ç¯ï¼
re-matcheré¢æ°ã«java.util.regex.Patternã¨æ£è¦è¡¨ç¾ãé©ç¨ããæååã渡ããã¨ã§ãjava.util.regex.Matcherãåå¾ãããã¨ãã§ãã¾ãã
(println (class (re-matcher number-pattern target-str))) ;; => java.util.regex.Matcher (println (class (re-matcher alpha-pattern target-str))) ;; => java.util.regex.Matcher (println (class (re-matcher not-match-pattern target-str))) ;; => java.util.regex.Matcher
re-find
re-findé¢æ°ã使ããã¨ã§ãæ£è¦è¡¨ç¾ã«ãããããæååãåå¾ãããã¨ãã§ãã¾ãã
(println (re-find number-pattern target-str)) ;; => 1234 (println (re-find alpha-pattern target-str)) ;; => Hello (println (re-find not-match-pattern target-str)) ;; => nil (println (re-find (re-matcher number-pattern target-str))) ;; => 1234 (println (re-find (re-matcher alpha-pattern target-str))) ;; => Hello (println (re-find (re-matcher not-match-pattern target-str))) ;; => nil (let [m (re-matcher number-pattern target-str)] (loop [match (re-find m)] (when match (println (str "matched => " match)) ;; => 1åç® 1234ã2åç® 5678 (recur (re-find m)))))
Matcherãªãã¸ã§ã¯ããã«ã¼ãã«æ¾ãè¾¼ããã¨ã§ããããããåã ãæååãåå¾ãã¦ãããã¨ãã§ãã¾ãã
ã°ã«ã¼ãåãããã¿ã¼ã³ã渡ãã¨ãçµæãã¡ãã£ã¨å¤ããã¾ãã
(def number-capture-pattern (re-pattern "(\\d+)")) (println (re-find number-capture-pattern target-str)) ;; => ["1234" "1234"] (println (re-find #"(\d+)" target-str)) ;; => ["1234" "1234"] (let [m (re-matcher number-capture-pattern target-str)] (loop [match (re-find m)] (when match (println (str "matched => " match)) ;; => 1åç® ["1234" "1234"]ã2åç® ["5678" "5678"] (recur (re-find m))))) (def number-capture-pattern (re-pattern "(\\d+)[^0-9]+(\\d+)")) (println (re-find number-capture-pattern target-str)) ;; => ["1234 World 5678" "1234" "5678"] (let [m (re-matcher number-capture-pattern target-str)] (loop [match (re-find m)] (when match (println (str "matched => " match)) ;; => ã«ã¼ãã¯1åã®ã¿ ["1234 World 5678" "1234" "5678"] (recur (re-find m)))))
çµæã¨ãã¦ãã¯ã¿ãè¿ãããã«ãªãããã¯ã¿ã®æåã®è¦ç´ ã¯ãã¿ã¼ã³ã«ãããããæååå ¨ä½ããã以éã¯ãã£ããã£ãããæååãç¶ãã¾ãã
re-seq
ãããããæååãããªã¹ãã¨ãã¦è¿ãã¾ãã
(def number-pattern #"\d+") (def number-capture-pattern #"(\d+)") (def alpha-number-capture-pattern #"([a-zA-Z]+) (\d+)") (println (re-seq number-pattern target-str)) ;; => ("1234" "5678") (println (re-seq number-capture-pattern target-str)) ;; => (["1234" "1234"] ["5678" "5678"]) (println (re-seq alpha-number-capture-pattern target-str)) ;; => (["Hello 1234" "Hello" "1234"] ["World 5678" "World" "5678"])
re-groups
re-matcherï¼re-findã¨çµã¿åããã¦ä½¿ç¨ãã¾ããjava.util.regex.Matcherã¸re-findé¢æ°ãé©ç¨ããå¾ã«ä½¿ããã¨ã§ããããããæååãåå¾ãããã¨ãã§ãã¾ããã°ã«ã¼ãåããå ´åã«ãã¯ã¿ãåå¾ã§ããããã«ãªãã®ã¯ãre-findãªã©ã¨åãã§ããéãã¯ãMatcherã«å¯¾ãã¦re-findãé©ç¨ãã¦ãããä½ç½®ãé²ããªãéããä½åº¦å®è¡ãã¦ãåããããçµæãåå¾ã§ãããã¨ã§ãã
(def number-pattern #"\d+") (def number-capture-pattern #"(\d+)") (def alpha-number-capture-pattern #"([a-zA-Z]+) (\d+)") (def number-matcher (re-matcher number-pattern target-str)) (def number-capture-matcher (re-matcher number-capture-pattern target-str)) (def alpha-number-capture-matcher (re-matcher alpha-number-capture-pattern target-str)) (re-find number-matcher) (re-find number-capture-matcher) (re-find alpha-number-capture-matcher) (println (re-groups number-matcher)) ;; => 1234 (println (re-groups number-capture-matcher)) ;; => ["1234" "1234"] (println (re-groups alpha-number-capture-matcher)) ;; => ["Hello 1234" "Hello" "1234"] (re-find number-matcher) (re-find number-capture-matcher) (re-find alpha-number-capture-matcher) (println (re-groups number-matcher)) ;; => 5678 (println (re-groups number-capture-matcher)) ;; => ["5678" "5678"] (println (re-groups alpha-number-capture-matcher)) ;; => ["World 5678" "World" "5678"]
ãªããMatcherã«å¯¾ãã¦re-findãé©ç¨ããã«re-groupsãé©ç¨ããã¨ãIllegalStateExceptionãé£ãã§ãã¾ãã
(try (println (re-groups (re-matcher number-pattern target-str))) (catch IllegalStateException e (println (.toString e)))) ;; => java.lang.IllegalStateException: No match found
re-matches
java.util.regex.Matcher#matchesã使ã£ã¦ã®æ£è¦è¡¨ç¾ã®é©ç¨ã¨ãªãã¾ããçµæã¯ãre-groupsã¨åãã§ãã
(def number-pattern #"\d+") (println (re-matches number-pattern target-str)) ;; => nil (def matches-pattern #"[a-zA-Z]+ \d+ [a-zA-Z]+ \d+") (println (re-matches matches-pattern target-str)) ;; => Hello 1234 World 5678 (def matches-capture-pattern #"([a-zA-Z]+) (\d+) ([a-zA-Z]+) (\d+)") (println (re-matches matches-capture-pattern target-str)) ;; => ["Hello 1234 World 5678" "Hello" "1234" "World" "5678"]
clojure.string/replace-first
æ£è¦è¡¨ç¾ã«ãããããæåã®æåã¾ãã¯æååã®ç½®æãè¡ãã¾ãã
(def target-str "Hello 1234 World 5678") (use '[clojure.string :only (replace-first)]) (println (replace-first target-str #"\d+" "NumberReplaced")) ;; => Hello NumberReplaced World 5678
ã°ã«ã¼ãåã使ç¨ããå ´åã¯ãã$nãã§å¾æ¹åç §ãè¡ããã¨ãã§ãã¾ãã
(println (replace-first target-str #"(\w+) \d+ (\w+)" "$1 $2")) ;; => Hello World 5678
第2å¼æ°ã¯ãjava.util.regex.Patternã§ãªãã¦ãæ§ãã¾ãããcharãStringã§ãOKã§ãã
(println (replace-first target-str "World" "Clojure")) ;; => Hello 1234 Clojure 5678 (println (replace-first target-str \o \O)) ;; => HellO 1234 World 567
(?i)ãä»ããã¨ãCase-Insensitiveã«ãªãã¾ãã
(println (replace-first target-str #"(?i)world" "Clojure")) ;; => Hello 1234 Clojure 5678
ã¡ãªã¿ã«ãClojureDocsã«ã¯é¢æ°ãå¼æ°ã«åã以ä¸ã®ãããªä¾ãè¼ã£ã¦ãã¾ããããä»åã¯è©¦ãã¦ã¾ããâ¦ã
user=> (import '(java.util.regex Matcher)) java.util.regex.Matcher user=> (defn re-qr [replacement] (Matcher/quoteReplacement replacement)) #'user/re-qr user=> (str/replace-first "fabulous fodder foo food" #"f(o+)(\S+)" (re-qr "$2$1")) "fabulous $2$1 foo food"
clojure.string/replace
使ãæ¹ã¯ãã»ã¼clojure.string/replace-firstã¨åãã§ããéãã¯ãreplace-firstãæåã®1åããç½®æããªãã®ã«å¯¾ããreplaceã¯ãããããåã ãç½®æãè¡ãã¾ãã
(println (clojure.string/replace target-str #"\d+" "NumberReplaced")) ;; => Hello NumberReplaced World NumberReplaced (println (clojure.string/replace target-str #"[a-zA-Z]+" "Clojure")) ;; => Clojure 1234 Clojure 5678 (println (clojure.string/replace target-str #"(\w+) \d+ (\w+)" "$1 $2")) ;; => Hello World 5678
ãªããuseããã¨clojure.core/replaceãé ãã¦ãã¾ãã®ã§ãè¦åãåºã¦ãã¾ãã¾ãã
(use '[clojure.string :only (replace)]) ;; WARNING: replace already refers to: #'clojure.core/replace in namespace: user, being replaced by: #'clojure.string/replace
ãã£ã¦ã
(clojure.string/replace target-str pattern replace-string))
ã¨ããå½¢ã«ãªãã¾ããâ¦ã¡ãã£ã¨å¾®å¦ã
clojure.string/split
ä¸ããããæ£è¦è¡¨ç¾ã§ãæååããã¯ã¿ã«åå²ãã¾ãã
(use '[clojure.string :only (split)]) (println (split target-str #"\s+")) ;; => ["Hello" "1234" "World" "5678"]
ããã®ãªãã±ã§ã\nã¾ãã¯\r\nã§åå²ããsplit-linesã¨ããé¢æ°ãããã¾ãã
(use '[clojure.string :only (split-lines)]) (println (split-lines "The\nQuick\r\nBrown\nFox")) ;; => ["The" "Quick" "Brown" "Fox"]
以ä¸ãæ£è¦è¡¨ç¾ä¸å·¡ãã§ãããã