在字符串中替换字符,用clojure中地图中的一组字符
我有一个功能,该函数添加了字符串s
和一个字符的地图charmap
。如果字符串s
中的任何字符都在charmap
的内部,请用地图的值替换字符。
注意,地图中的键必须是字符串,而不是字符串。
例如:
(replace-characters "Hi!" {"!" "Exclamation Mark"}) ;; => "HiExclamation Mark"
这是我当前正在使用的代码:
(defn- replace-characters
"Replaces any characters in a string that are mapped in the given charmap"
[s charmap]
(let [character-set (set s)
characters (filter #(contains? charmap (str %)) character-set)]
(string/replace s (re-pattern (string/join "|" characters)) charmap)))
但是,我得到了nullpointerexception
,我对原因感到非常困惑。
java.lang.NullPointerException: Cannot invoke "String.indexOf(int)" because "s" is null
我想在纯clojure中解决这个问题,最好是Java等
。这很好。但是由于某种原因,以下原因是导致错误:
(-> s
(string/replace #"[:/?#\[\]@!$&'()*+,;=]" "")
(replace-characters charmap)) ;; Where charmap is a large map of key value characters.
I have a function that takes in a string s
and a map of characters charmap
. If any characters in the string s
are inside of charmap
, replace the character with the value of the map.
Note, the keys in the map must be a string, rather than a char.
For example:
(replace-characters "Hi!" {"!" "Exclamation Mark"}) ;; => "HiExclamation Mark"
This is the code I am currently using:
(defn- replace-characters
"Replaces any characters in a string that are mapped in the given charmap"
[s charmap]
(let [character-set (set s)
characters (filter #(contains? charmap (str %)) character-set)]
(string/replace s (re-pattern (string/join "|" characters)) charmap)))
However, I am getting a NullPointerException
and I am seriously confused on why.
java.lang.NullPointerException: Cannot invoke "String.indexOf(int)" because "s" is null
I would like to solve this in pure Clojure preferably, not Java, etc.
Update: So the code above works in the repl. Which is nice. But for some reason the following is causing the error:
(-> s
(string/replace #"[:/?#\[\]@!amp;'()*+,;=]" "")
(replace-characters charmap)) ;; Where charmap is a large map of key value characters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是导致错误的表达式:(
“!”
被正则替换为“”
, targine> targine-set 具有值## {\ h \ i}
和字符
is (),因此使用re repattern
IS###创建的模式“
。)空的正则态度与字母之间的每个空间匹配:
so,
替换
正在寻找在哈希映射{“!”中替换。 “感叹号”}
,但找不到任何东西 - 没有键“”
:一种可能的解决方案可以简化
repent> replace-characters
的定义。 (此解决方案仅适用于非Expry-Code> CharMap ):测试:
This is the expression that causes error:
(
"!"
was replaced with regex to""
,character-set
has value#{\H \i}
andcharacters
is()
, so pattern created withre-pattern
is#""
.)Empty regex matches every space between letters:
So,
replace
is looking for replacement in hash-map{"!" "Exclamation Mark"}
, but doesn't find anything- there is no key""
:One possible solution can be to simplify definition of
replace-characters
(this solution will work only for non-emptycharmap
):Tests:
我只会和这样简单的事情:
或这样使用传感器:
或者可能是这样:
i would just go with something as simple as this:
or this way with transducers:
or maybe like this:
string> string/code
确切地做您想要的事情:该功能替代字符使用从角色到更换的地图。
如果要替换REGEXES或字符串,则可以使用
redain-kv
< < < < < /a>与string/替换
(按顺序,当
替换
是 /code> )在替换的密钥和值上,并使用String/replact
在字符串s
上应用它们。如果
array-map
中的元素的顺序颠倒了!使用“”
(因为!在以下是正则>)将成功:string/escape
does exactly what you want:that function replaces characters using a map from character to replacement.
If you want to replace regexes or string you can use
reduce-kv
in combination withstring/replace
:So you loop (in order, when
replacements
is anarray-map
) over the key and values of the replacements and apply them on the strings
usingstring/replace
.if the order of the elements in the
array-map
is reversed the substitution of the ! with""
(since ! is in the regex) would succeed: