Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 在Clojure中将*替换为\*_String_Clojure_Replace - Fatal编程技术网

String 在Clojure中将*替换为\*

String 在Clojure中将*替换为\*,string,clojure,replace,String,Clojure,Replace,在clojure中如何将“*”转义到“\*”?似乎无法让它工作: (s/replace“A*B”#“*”*”产生“A*B”(当然) (s/replace“A*B”#“*”\*”失败:不支持的转义字符: (s/replace“A*B”#“*”\\\*”再次生成“A*B” (s/replace“A*B”\\\*”失败:不支持的转义字符:再次 (s/replace“A*B”\\\\*“*”产生“A\\*B” 我无法让它产生A\*B 有什么想法吗?谢谢您必须使用4个反斜杠: > (println

在clojure中如何将
“*”
转义到
“\*”
?似乎无法让它工作:

(s/replace“A*B”#“*”*”
产生
“A*B”
(当然)

(s/replace“A*B”#“*”\*”
失败:
不支持的转义字符:

(s/replace“A*B”#“*”\\\*”
再次生成
“A*B”

(s/replace“A*B”\\\*”
失败:
不支持的转义字符:
再次

(s/replace“A*B”\\\\*“*”
产生
“A\\*B”

我无法让它产生
A\*B

有什么想法吗?谢谢

您必须使用4个反斜杠:

> (println (clojure.string/replace "A*B" #"\*" "\\\\*"))
A\*B
nil
>  
或者,没有正则表达式,它只是:

> (println (clojure.string/replace "A*B" "*" "\\*"))
A\*B
nil
>  

要将其用作正则表达式模式,请使用以下函数:

> (def p (clojure.string/replace "A*B" #"\*" "\\\\*"))
#'sandbox17459/p
> (println p)
A\*B
nil
> (clojure.string/replace "BLA*BLA" (re-pattern p) "UH")
"BLUHLA"
>  

这仅在打印结果时有效:user=>(clojure.string/replace“AB”\\*“*”\\\*”)A\*B“user=>(clojure.string/replace“AB”*“\*”)“A\*B”我必须在正则表达式中使用结果。我不想打印它。我想将“BLA*BLA”替换为“BLA\*BLA”-这正是字符串。可悲的是,你给我的例子不起作用……好吧。所以你想用一个正则表达式来创建另一个正则表达式模式?您能给出一个输入示例、您期望的正则表达式模式以及您期望的结果吗?我有一个字符串,其中包含Glassfish提供的jvm选项列表。其中一个是这样的:-blabla,excludes=*com/sun/corba**/*。我需要在列表中找到此文本。我想将“BLABLA”替换为“BLA\*BLA”:
(clojure.string/replace“BLA*BLA”\*“\*”“\\\\*”
执行此操作。为了澄清起见,请注意,您不能让像
“BLA\*BLA”
这样的字符串文字在\之间流动。\更改以下字符的含义,将其转换为转义字符。因此\*必须在字符串中自身转义。如果要将字符串
“BLA\\*BLA”
用作正则表达式模式
BLA\*BLA
,则必须使用
re-pattern
函数,因为
“BLA\*BLA”
不是有效字符串(因为\不是字符串中的有效转义字符)。