在 Haskell中使用带有选项(标志)的正则表达式的最佳方法是什么
我用
Text.Regex.PCRE
文档列出了一些有趣的选项,如compCaseless,compUTF8,…
但我不知道如何使用它们(=〜)
所有Text.Regex.*模块都大量使用类型类,它们具有可扩展性和“重载”类似的行为,但仅仅看到类型就不那么明显了.
现在,你可能已经从基本=〜匹配器开始了.
进群:960410445 即可获取书十套PDF!
![5b3bc6e939a970f5464d4fbadd15db17.png](https://img-blog.csdnimg.cn/img_convert/5b3bc6e939a970f5464d4fbadd15db17.png)
(=~) :: ( 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
要使用=〜,必须存在RegexMaker的实例…用于LHS,RegexContext …用于RHS和结果.
class RegexOptions regex compOpt execOpt | ... | regex -> compOpt execOpt , compOpt -> regex execOpt , execOpt -> regex compOptclass 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
所有这些类的有效实例(例如,regex = Regex,compOpt = CompOption,execOpt = ExecOption和source = String)意味着可以使用某些表单源的compOpt,execOpt选项编译正则表达式. (另外,给定一些正则表达式类型,只有一个compOpt,execOpt设置随之而来.但是很多不同的源类型都可以.)
class Extract sourceclass Extract source => RegexLike regex sourceclass RegexLike regex source => RegexContext regex source target where match :: regex -> source -> target matchM :: Monad m => regex -> source -> m target
所有这些类的有效实例(例如,regex = Regex,source = String,target = Bool)意味着可以匹配源和正则表达式以产生目标. (给定这些特定正则表达式和源的其他有效目标是Int,MatchResult String,MatchArray等)
把这些放在一起很明显=〜和= ~~只是简单的功能
source1 =~ source = match (makeRegex source) source1source1 =~~ source = matchM (makeRegex source) source1
而且= =和= ~~没有空间来传递makeRegexOpts的各种选项.
你可以做自己的
(=~+) :: ( RegexMaker regex compOpt execOpt source , RegexContext regex source1 target ) => source1 -> (source, compOpt, execOpt) -> targetsource1 =~+ (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 targetsource1 =~~+ (source, compOpt, execOpt) = matchM (makeRegexOpts compOpt execOpt source) source1
可以像
"string" =~+ ("regex