不区分大小写的正则表达式 [英] case-insensitive regular expressions

查看:170
本文介绍了不区分大小写的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用

<$ p

在Haskell中使用带有选项(标志)的正则表达式的最佳方式是什么? $ 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 等)

把这些放在一起很明显, =〜 = ~~

code>只是简单的便利函数

  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 targets 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆