不区分大小写的正则表达式 [英] case-insensitive regular expressions
问题描述
我使用
<$ p
Text.Regex.PCRE
文档列出了一些有趣的选项像compCaseless,compUTF8,...
但我不知道如何使用它们(=〜)
所有的 Text.Regex。*
模块都大量使用了类型类,这些类是用于扩展性和重载类行为,但是从看不见类型。
现在,您可能已经从基本的 =〜
匹配器开始了。
(=〜)::
(RegexMaker正则表达式CompOption ExecOption源
,RegexContext正则表达式source1目标)
=> source1 - >来源 - >目标
(= ~~)::
(RegexMaker Regex CompOption ExecOption source
,RegexContext Regex source1 target,Monad m)
=> source1 - >来源 - > m目标
要使用 =〜〜
,那里对于LHS,必须存在一个 RegexMaker ...
的实例,对于RHS和结果,必须存在 RegexContext ...
。
class RegexOptions regex compOpt execOpt | ...
|正则表达式 - > compOpt execOpt
,compOpt - >正则表达式execOpt
,execOpt - >正则表达式compOpt
类RegexOptions正则表达式compOpt execOpt
=> RegexMaker正则表达式compOpt execOpt源
|正则表达式 - > compOpt execOpt
,compOpt - >正则表达式execOpt
,execOpt - >正则表达式compOpt
其中
makeRegex :: source - >正则表达式
makeRegexOpts :: compOpt - > execOpt - >来源 - >正则表达式
所有这些类的有效实例(例如, regex = Regex ,
compOpt = CompOption
, execOpt = ExecOption
和来源= String
)意味着可以使用 compOpt,execOpt
选项编译 regex
形式源
。 (另外,给定一些 regex
类型,只有一个 compOpt,execOpt
集合会随之而来。不同的 source
类型也可以。)
class Extract source
class提取源
=> RegexLike正则表达式源
类RegexLike正则表达式源
=> RegexContext正则表达式源目标
其中
match :: regex - >来源 - >目标
matchM :: Monad m =>正则表达式 - >来源 - > m目标
所有这些类的有效实例(例如, 把这些放在一起很明显, regex =正则表达式
, source = String ,
target = Bool
)意味着可以匹配 source
和一个 regex
来产生一个目标
。 (其他有效的 target
s给定了这些特定的 regex
和 source
是 Int
, MatchResult字符串
, MatchArray
等)
=〜
和 = ~~
source1 =〜源
=匹配(makeRegex源代码)source1
source1 = ~~ source
= matchM(makeRegex source)source1
=〜
和 = ~~
请不要将各种选项传递给 makeRegexOpts
你可以自己制作
( =〜+)::
(RegexMaker正则表达式compOpt execOpt源
,RegexContext正则表达式source1目标)
=> source1 - > (source,compOpt,execOpt) - > target
source1 =〜+(source,compOpt,execOpt)
= match(makeRegexOpts compOpt execOpt source)source1
(= ~~ +)::
(RegexMaker regex compOpt execOpt源
,RegexContext regex source1 target,Monad m)
=> source1 - > (source,compOpt,execOpt) - > m target
source1 = ~~ +(source,compOpt,execOpt)
= matchM(makeRegexOpts compOpt execOpt source)source1
可以像
string=〜+(regex, CompCaseless + compUTF8,execBlank):: Bool
或覆盖 =〜
和 = ~~
与可接受选项的方法
import Text.Regex.PCRE hiding((=〜),(= ~~))
class RegexSourceLike regex source
where
makeRegexWith source :: source - >正则表达式
实例RegexMaker正则表达式compOpt execOpt源
=> RegexSourceLike正则表达式源
其中
makeRegexWith = makeRegex
实例RegexMaker正则表达式compOpt execOpt源
=> RegexSourceLike regex(source,compOpt,execOpt)
其中
makeRegexWith(source,compOpt,execOpt)
= makeRegexOpts compOpt execOpt源
$ b $ source1 =〜源
= match(makeRegexWith source)source1
source1 = ~~ source
= matchM(makeRegexWith source)source1
或者您可以直接在需要的地方直接使用 match
, makeRegexOpts
等等。 p>
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 (=~)
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.
(=~) ::
( RegexMaker Regex CompOption ExecOption source
, RegexContext Regex source1 target )
=> source1 -> source -> target
(=~~) ::
( RegexMaker Regex CompOption ExecOption source
, RegexContext Regex source1 target, Monad m )
=> source1 -> source -> m target
To use =~
, there must exist an instance of RegexMaker ...
for the LHS, and RegexContext ...
for the RHS and result.
class RegexOptions regex compOpt execOpt | ...
| regex -> compOpt execOpt
, compOpt -> regex execOpt
, execOpt -> regex compOpt
class RegexOptions regex compOpt execOpt
=> RegexMaker regex compOpt execOpt source
| regex -> compOpt execOpt
, compOpt -> regex execOpt
, execOpt -> regex compOpt
where
makeRegex :: source -> regex
makeRegexOpts :: compOpt -> execOpt -> source -> regex
A valid instance of all these classes (for example, regex=Regex
, compOpt=CompOption
, execOpt=ExecOption
, and source=String
) means it's possible to compile a regex
with compOpt,execOpt
options from some form source
. (Also, given some regex
type, there is exactly one compOpt,execOpt
set that goes along with it. Lots of different source
types are okay, though.)
class Extract source
class Extract source
=> RegexLike regex source
class RegexLike regex source
=> RegexContext regex source target
where
match :: regex -> source -> target
matchM :: Monad m => regex -> source -> m target
A valid instance of all these classes (for example, regex=Regex
, source=String
, target=Bool
) means it's possible to match a source
and a regex
to yield a target
. (Other valid target
s given these specific regex
and source
are Int
, MatchResult String
, MatchArray
, etc.)
Put these together and it's pretty obvious that =~
and =~~
are simply convenience functions
source1 =~ source
= match (makeRegex source) source1
source1 =~~ source
= matchM (makeRegex source) source1
and also that =~
and =~~
leave no room to pass various options to makeRegexOpts
.
You could make your own
(=~+) ::
( RegexMaker regex compOpt execOpt source
, RegexContext regex source1 target )
=> source1 -> (source, compOpt, execOpt) -> target
source1 =~+ (source, compOpt, execOpt)
= match (makeRegexOpts compOpt execOpt source) source1
(=~~+) ::
( RegexMaker regex compOpt execOpt source
, RegexContext regex source1 target, Monad m )
=> source1 -> (source, compOpt, execOpt) -> m target
source1 =~~+ (source, compOpt, execOpt)
= matchM (makeRegexOpts compOpt execOpt source) source1
which could be used like
"string" =~+ ("regex", CompCaseless + compUTF8, execBlank) :: Bool
or overwrite =~
and =~~
with methods which can accept options
import Text.Regex.PCRE hiding ((=~), (=~~))
class RegexSourceLike regex source
where
makeRegexWith source :: source -> regex
instance RegexMaker regex compOpt execOpt source
=> RegexSourceLike regex source
where
makeRegexWith = makeRegex
instance RegexMaker regex compOpt execOpt source
=> RegexSourceLike regex (source, compOpt, execOpt)
where
makeRegexWith (source, compOpt, execOpt)
= makeRegexOpts compOpt execOpt source
source1 =~ source
= match (makeRegexWith source) source1
source1 =~~ source
= matchM (makeRegexWith source) source1
or you could just use match
, makeRegexOpts
, etc. directly where needed.
这篇关于不区分大小写的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!