我正在寻找一种将注释嵌入到Clojure中的正则表达式中的方法,以解释这段代码或这段代码的作用。我在文档中找不到此类功能。
Clojure中具有以下正则表达式:
#"\$[A-Z]+|\((?:(?:\$[A-Z]+|[\w\'\-\_]+)(?:\|(?:\$[A-Z]+|[\w\'\-\_]+))*)\)"
Run Code Online (Sandbox Code Playgroud)
如何在正则表达式中添加注释?在Perl中,我会坚持使用尾随/x修饰符,例如:
$_ =~ m/ abc # some comment explaining what abc is about
/x;
Run Code Online (Sandbox Code Playgroud)
这样几天后就可以轻松处理它。
我该如何在Clojure中进行操作?
您可以使用内嵌标志选项(或内联修饰符)(?x):
(str #"(?x) # Turn on COMMENTS mode
\$[A-Z]+| # $ and 1+ ASCII letters or
\( # ( char
(?: # Start of a non-capturing group:
(?:\$[A-Z]+|[\w'-]+) # $ and 1+ ASCII letters or 1+ word, ' or - chars
(?: # Start of a non-capturing group:
\|(?:\$[A-Z]+|[\w'-]+) # |, $ and 1+ ASCII letters or 1+ word, ' or - chars
)* # End of the inner non-capturing group, repeat 0 or more times
) # End of the outer non-capturing group
\) # ) char
"
)
Run Code Online (Sandbox Code Playgroud)
注意:
因此即使在字符类内部,也必须转义所有文字常规空格。#在模式中使用文字,请对其进行转义。| 归档时间: |
|
| 查看次数: |
100 次 |
| 最近记录: |