如何转义 Haskell 的 Text.Regex 库中的字符?
简介
我正在使用 Haskell 的 Text.Regex
库,并且我想匹配一些通常在正则表达式中有意义的字符。根据 Text.Regex
的 文档,
正则表达式的语法是...egrep 的语法(即 POSIX“扩展”正则表达式)。
显然,在 POSIX 扩展正则表达式 (ERE) 中转义使用反斜杠 [与 POSIX 基本正则表达式(BRE)]。
问题
但是,当我尝试做这样的事情时:
> import Text.Regex
> matchRegex (mkRegex "\*") "*"
我收到以下错误:
<interactive>:1:23:
lexical error in string/character literal at character '*'
无论我在 \
之后放置什么字符,都会发生同样的事情。
解决方法
我可以做这样的事情:
> matchRegex (mkRegex "[*]") "*"
Just []
它有效,但看起来像是一个黑客,特别是如果我想连续转义几件事(例如 mkRegex "[[][(][)][]] "
与 [()]
匹配)。
问题
这是 POSIX ERE 中逃脱的唯一方法吗?为什么 Haskell 的 Text.Regex
库不支持 \
转义,就像它看起来应该的那样?
Introduction
I'm using Haskell's Text.Regex
library and I want to match some characters that normally have meaning in regular expressions. According to Text.Regex
's documentation,
The syntax of regular expressions is ... that of egrep (i.e.
POSIX "extended" regular expressions).
And apparently, escaping in POSIX Extended Regular Expressions (ERE) uses backslashes [unlike POSIX Basic Regular Expressions (BRE)].
Problem
However, when I try to do something like this:
> import Text.Regex
> matchRegex (mkRegex "\*") "*"
I get the following error:
<interactive>:1:23:
lexical error in string/character literal at character '*'
The same thing happens no matter what character I put after the \
.
Work-Around
I could do something like this:
> matchRegex (mkRegex "[*]") "*"
Just []
which works, but it seems like a hack, especially if I want to escape several things in a row (e.g. mkRegex "[[][(][)][]]"
which matches [()]
).
Question
Is this the only way to escape in POSIX ERE? Why doesn't Haskell's Text.Regex
library support \
escaping like it seems it ought to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道语法,但通常如果你想在字符串中写反斜杠,你需要转义它,意思是:
它有帮助吗?
I don't know the syntax but usually if you want to write back-slash inside a string you need to escape it, meaning:
Does it help?
用两个反斜杠尝试一下:
我刚刚用 GHCI 尝试过,它有效。
Try it with two backslashes:
I just tried that with GHCI and it worked.