如何编写一个Clojure宏来从String创建正则表达式? [英] How do I write a Clojure macro to create a regular expression from a String?
问题描述
我创建了一个方便的宏。部分方便的是,正则表达式可以只用一个String来指定,而不是#re符号。
I'm creating a convenience macro. Part of the convenience is that a regular expression can be specified with just a String, rather than the #"re" notation.
一个部分我不知道是如何获取宏接受字符串和重写它作为一个Clojure regex(例如,产生#re符号)。我认为这是一个语法/转义问题。
The one part I can't figure out is how to get the macro to take the String and rewrite it as a Clojure regex (e.g., produce the #"re" notation). I think it's a syntax / escaping problem.
我的第一个天真的尝试(假装我只想要字符串到正则表达式部分):
My first naive attempt (pretending I only want the String-to-regex part):
(defmacro mymac [mystr] `#~mystr)
b $ b
甚至可以做我想做的事吗?或者,是否有一个实际的函数来接受一个字符串并产生一个正则表达式,而不是使用#读取器宏?
Is it even possible to do what I'm trying to do? Or, is there an actual function to take a String and produce a regex, instead of using the # reader macro?
或者我应该放入Java并使用java。 util.regex.Pattern?
Or should I just drop into Java and use java.util.regex.Pattern?
推荐答案
有一个函数:重新模式
user=> (re-pattern "\\d+")
#"\d+"
这篇关于如何编写一个Clojure宏来从String创建正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!