Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 35 additions and 25 deletions.
  1. +23 −19 clojure/core.clj
  2. +12 −6 tests/re-tests.clj
View
42 clojure/core.clj
@@ -3428,39 +3428,43 @@
[x] (instance? FunctionType x))
(require 're)
-(def PatternType (py/type (re/compile "")))
(defn re-pattern
- "Returns a compiled Python Pattern object, for use, e.g. in
+ "Accepts a compiled regex or a string containing a regex pattern.
+ Returns a compiled Python Pattern object, for use, e.g. in
re-matcher."
{:added "1.0"
:static true}
- [s] (if (instance? PatternType s)
- s
- (re/compile s)))
+ [s] (re/compile s))
(defn re-matcher
- "Returns a Python MatchObject, for use, e.g. in
+ "Accepts a compiled regex or a string containing a regex pattern.
+ Returns a Python MatchObject, for use, e.g. in
re-find. If no match, returns nil."
{:added "1.0"
:static true}
- [^PatternType re s]
- (.search re s))
+ [re s] (re/search re s))
-(def MatchObjectType (py/type (.match (re-pattern "^.*$") "foo")))
(defn re-groups
"Returns the groups from the most recent match/find. If there are no
nested groups, returns a string of the entire match. If there are
nested groups, returns a vector of the groups, the first element
- being the entire match."
+ being the entire match. Can also take a string containing a regular
+ expression, instead of a compiled regex."
{:added "1.0"
:static true}
- [^MatchObjectType m]
- (if (nil? m)
- []
- (let [gc (count (.groups m))]
- (loop [ret [] c 0]
- (if (<= c gc)
- (recur (conj ret (.group m c)) (inc c))
- ret)))))
-
+ [m]
+ (if (nil? m)
+ []
+ (let [gc (count (.groups m))]
+ (loop [ret [] c 0]
+ (if (<= c gc)
+ (recur (conj ret (.group m c)) (inc c))
+ ret)))))
+
+(defn re-seq
+ "Returns a sequence of successive matches of pattern in string,
+ using re.findall()."
+ {:added "1.0"
+ :static true}
+ [re s] (re/findall re s))
View
18 tests/re-tests.clj
@@ -7,8 +7,9 @@
(def PatternType (py/type (re/compile "")))
(assertions/assert-equal PatternType (py/type (re-pattern "")))
(assertions/assert-equal PatternType (py/type (re-pattern "foo")))
- (assertions/assert-true (instance? PatternType (re-pattern "foo")))
- (assertions/assert-true (instance? PatternType (re-pattern "^(.*)(foo)(bar)+$"))))
+ (assertions/assert-true (instance? PatternType (re-pattern "foo")))
+ (assertions/assert-true (instance? PatternType (re-pattern (re-pattern "foo"))))
+ (assertions/assert-true (instance? PatternType (re-pattern "^(.*)(foo)(bar)+$"))))
(deftest re-matcher-tests
(def MatchObjectType (py/type (.search (re-pattern "^.*$") "foo")))
@@ -21,10 +22,11 @@
(assertions/assert-true (instance? MatchObjectType get-matcher))
(assertions/assert-equal "foo-bar" (.group get-matcher 0))
(assertions/assert-equal "foo" (.group get-matcher 1))
- (assertions/assert-equal "bar" (.group get-matcher 2)))
+ (assertions/assert-equal "bar" (.group get-matcher 2))
+ (assertions/assert-not-nil (re-matcher "foo" "foobar"))
+ (assertions/assert-equal "foo" (.group get-matcher 1)))
(deftest re-groups-tests
- (assertions/assert-equal [] [])
(assertions/assert-equal [""] (re-groups (re-matcher (re-pattern "") "")))
(assertions/assert-equal [""] (re-groups (re-matcher (re-pattern "") "foo")))
(assertions/assert-equal [] (re-groups (re-matcher (re-pattern "foo") "")))
@@ -32,6 +34,10 @@
(assertions/assert-equal ["foo" "foo"] (re-groups (re-matcher (re-pattern "(foo)") "foo")))
(assertions/assert-equal ["foobar" "foo" "bar"] (re-groups (re-matcher (re-pattern "(foo)(bar)") "foobar")))
(assertions/assert-equal ["foobbbbbarrrbbbaaazzz" "foo" "bbbbbarrr" "bbbaaazzz"] (re-groups (re-matcher (re-pattern "(fo+)(b+.r*)(b*a*z*)") "foobbbbbarrrbbbaaazzz")))
- (assertions/assert-equal ["foobarbaz" "foo" "barbaz" "baz"] (re-groups (re-matcher (re-pattern "(foo)(bar(baz))") "foobarbaz")))
-)
+ (assertions/assert-equal ["foobarbaz" "foo" "barbaz" "baz"] (re-groups (re-matcher (re-pattern "(foo)(bar(baz))") "foobarbaz"))))
+(deftest re-seq-tests
+ (assertions/assert-equal ["1" "1" "1"] (re-seq (re-pattern "(1)") "111"))
+ (assertions/assert-equal [["111" "222"]] (re-seq (re-pattern "(1+)(2+)") "111222"))
+ (assertions/assert-equal ["121" "343" "565"] (re-seq (re-pattern "(\\d+)") "121baz343foo-565bar"))
+)

No commit comments for this range

Something went wrong with that request. Please try again.