Clojure是否有一个Split函数将String拆分为包含分隔符的子字符串? 像“a = b”,分隔符“=” return:“a”,“=”,“b”。 谢谢!
答案 0 :(得分:2)
我发现regexp是最简单的变体:
class Article(db.Model, Base):
id = db.Column(db.Integer(), primary_key=True)
title = db.Column(db.String())
description = db.Column(db.Text())
url = db.Column(db.String())
created_on = db.Column(db.DateTime(), server_default=db.func.now())
updated_on = db.Column(db.DateTime(), server_default=db.func.now(), onupdate=db.func.now())
author = db.Column(db.Integer(), db.ForeignKey('user.id'))
category = db.Column(db.Integer(), db.ForeignKey('category.id'))
def __init__(self, title="", description="", author="", category=""):
self.title = title
self.author = author
self.category = category
self.description = description
self.url = slugify(title)
答案 1 :(得分:1)
我不是很清楚,但您可以使用interpose
完成此操作:
user=> (def mystring "a=b=cde=fg=hij")
#'user/mystring
user=> (interpose "=" (clojure.string/split mystring #"="))
("a" "=" "b" "=" "cde" "=" "fg" "=" "hij")
答案 2 :(得分:1)
split-with
主要是做这件事,虽然这需要你做一点工作。
(split-with #(not= \= %) "a=b")
产量
[(\a) (\= \b)]
我能想到解决这个问题的最惯用的事情是:
(->> "a=b=c=d" ; Thread the string through the last argument of...
(split-with #(not= \= %)) ; Splitting on =
(flatten) ; Then flattening
(map str)) ; And turning the characters into strings
("a" "=" "b" "=" "c" "=" "d")
由于flatten
,这可能不会有效,所以如果在长列表中不断调用它,这将不实用。
答案 3 :(得分:0)
(defn split-but-keep
"Sep must be escaped str (er, double escaped actually).
Use `|` separated seps for multiple e.g. \\(|\\) as a str for open or close paren"
[s sep]
(let [re (re-pattern (str "[^" sep "]+|" sep))]
(re-seq re s)))
这只是@leetwinski的答案
答案 4 :(得分:0)
使用 clojure.string/split
,但在整个拆分正则表达式周围放置一个组。
cljs.user=> (def my-regex-with-group #"(=)")
#'cljs.user/my-regex-with-group
cljs.user=> (require '[clojure.string :as s])
nil
cljs.user=> (s/split "ab=dd=cb" my-regex-with-group)
["ab" "=" "dd" "=" "cb"]
如您所见,这也适用于 Clojurescript。正则表达式可以是任何有效的正则表达式。
cljs.user=> (s/split "ab=dd=cb" #"(dd=)")
["ab=" "dd=" "cb"]
没有组,匹配被省略
cljs.user=> (s/split "ab=dd=cb" #"dd=")
["ab=" "cb"]