不区分大小写的正则表达式
Haskell 中使用带选项(标志)的正则表达式的最佳方式是什么
在我使用的
Text.Regex.PCRE
该文档列出了一些有趣的选项,如 compCaseless、compUTF8、... 但我不知道如何使用它们 (=~)
What's the best way to use regular expressions with options (flags) in Haskell
I use
Text.Regex.PCRE
The documentation lists a few interesting options like compCaseless, compUTF8, ...
But I don't know how to use them with (=~)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
所有
Text.Regex.*
模块都大量使用类型类,这些类型类的存在是为了可扩展性和类似“重载”的行为,但仅从类型来看,其用法不太明显。现在,您可能已经从基本的
=~
匹配器开始了。要使用
=~
,必须存在用于 LHS 的RegexMaker ...
实例,以及用于 RHS 和结果的RegexContext ...
实例。所有这些类的有效实例(例如,
regex=Regex
、compOpt=CompOption
、execOpt=ExecOption
和source= String
) 意味着可以从某种形式的source
中使用compOpt,execOpt
选项编译regex
。 (此外,给定某种regex
类型,只有一个compOpt,execOpt
集与之配套。许多不同的source
类型都可以不过。)所有这些类的有效实例(例如,
regex=Regex
、source=String
、target=Bool
)意味着它是可以匹配源
和正则表达式
来生成目标
。 (给定这些特定的regex
和source
的其他有效的target
是Int
、MatchResult String
>、MatchArray
等)将它们放在一起,很明显
=~
和=~~
只是方便的函数,而且 < code>=~ 和
=~~
没有空间将各种选项传递给makeRegexOpts
。您可以自己制作
它,
或者使用可以接受选项的方法覆盖
=~
和=~~
,或者您可以只使用
match
>、makeRegexOpts
等直接在需要的地方。All the
Text.Regex.*
modules make heavy use of typeclasses, which are there for extensibility and "overloading"-like behavior, but make usage less obvious from just seeing types.Now, you've probably been started off from the basic
=~
matcher.To use
=~
, there must exist an instance ofRegexMaker ...
for the LHS, andRegexContext ...
for the RHS and result.A valid instance of all these classes (for example,
regex=Regex
,compOpt=CompOption
,execOpt=ExecOption
, andsource=String
) means it's possible to compile aregex
withcompOpt,execOpt
options from some formsource
. (Also, given someregex
type, there is exactly onecompOpt,execOpt
set that goes along with it. Lots of differentsource
types are okay, though.)A valid instance of all these classes (for example,
regex=Regex
,source=String
,target=Bool
) means it's possible to match asource
and aregex
to yield atarget
. (Other validtarget
s given these specificregex
andsource
areInt
,MatchResult String
,MatchArray
, etc.)Put these together and it's pretty obvious that
=~
and=~~
are simply convenience functionsand also that
=~
and=~~
leave no room to pass various options tomakeRegexOpts
.You could make your own
which could be used like
or overwrite
=~
and=~~
with methods which can accept optionsor you could just use
match
,makeRegexOpts
, etc. directly where needed.我对 Haskell 一无所知,但如果您使用基于 PCRE 的正则表达式库,那么您可以在正则表达式中使用模式修饰符。 要以不区分大小写的方式匹配“无大小写”,您可以在 PCRE 中使用此正则表达式:
模式修饰符 (?i) 会覆盖在正则表达式外部设置的任何区分大小写或不区分大小写的选项。 它也适用于不允许您设置任何选项的运算符。
类似地,(?s) 打开“单行模式”,使点匹配换行符,(?m) 打开“多行模式”,使 ^ 和 $ 在换行符处匹配,(?x) 打开自由-spacing 模式(字符类之外的未转义空格和换行符无关紧要)。 您可以组合字母。 (?ismx) 打开一切。 连字符关闭选项。 (?-i) 使正则表达式区分大小写。 (?xi) 启动一个自由间距区分大小写的正则表达式。
I don't know anything about Haskell, but if you're using a regex library based on PCRE, then you can use mode modifiers inside the regular expression. To match "caseless" in a case insensitive fashion, you can use this regex in PCRE:
The mode modifier (?i) overrides any case sensitivity or case insensitivity option that was set outside the regular expression. It also works with operators that don't allow you to set any options.
Similarly, (?s) turns on "single line mode" which makes the dot match line breaks, (?m) turns on "multi line mode" which makes ^ and $ match at line breaks, and (?x) turns on free-spacing mode (unescaped spaces and line breaks outside character classes are insignificant). You can combine the letters. (?ismx) turns on everything. A hyphen turns off options. (?-i) makes the regex case sensitive. (?x-i) starts a free-spacing case sensitive regex.
我相信如果您希望使用
defaultCompOpt
之外的compOpt
,则不能使用 (=~)。像这样的工作:
以下两篇文章应该可以帮助您:
现实世界 Haskell,第 8 章。高效文件处理、正则表达式和文件名匹配
Haskell 正则表达式教程
I believe cannot use (=~) if you wish to use
compOpt
other thandefaultCompOpt
.Something like this work:
The follow two articles should assist you:
Real World Haskell, Chapter 8. Efficient file processing, regular expressions, and file name matching
A Haskell regular expression tutorial