正则表达式 – 不区分大小写的正则表达式
source link: https://codeday.me/bug/20190112/510681.html?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
中使用带有选项(标志)的正则表达式的最佳方法是什么
我用
Text.Regex.PCRE
文档列出了一些有趣的选项,如compCaseless,compUTF8,…
但我不知道如何使用它们(=〜)
所有Text.Regex.*模块都大量使用类型类,它们具有可扩展性和“重载”类似的行为,但仅仅看到类型就不那么明显了.
现在,你可能已经从基本=〜匹配器开始了.
(=~) :: ( 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 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
所有这些类的有效实例(例如,regex = Regex,compOpt = CompOption,execOpt = ExecOption和source = String)意味着可以使用某些表单源的compOpt,execOpt选项编译正则表达式. (另外,给定一些正则表达式类型,只有一个compOpt,execOpt设置随之而来.但是很多不同的源类型都可以.)
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
所有这些类的有效实例(例如,regex = Regex,source = String,target = Bool)意味着可以匹配源和正则表达式以产生目标. (给定这些特定正则表达式和源的其他有效目标是Int,MatchResult String,MatchArray等)
把这些放在一起很明显=〜和= ~~只是简单的功能
source1 =~ source = match (makeRegex source) source1 source1 =~~ source = matchM (makeRegex source) source1
而且= =和= ~~没有空间来传递makeRegexOpts的各种选项.
你可以做自己的
(=~+) :: ( 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
可以像
"string" =~+ ("regex", CompCaseless + compUTF8, execBlank) :: Bool
或者使用可以接受选项的方法覆盖=〜和= ~~
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
或者您可以直接在需要的地方使用match,makeRegexOpts等.
翻译自:https://stackoverflow.com/questions/1007846/case-insensitive-regular-expressions
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK