Haskell の正規表現すげー !!

効率的なファイル操作、正規表現、ファイル名マッチング
(2012/02/18 リンク切れ)
翻訳していただきありがとうございます。

(=~) の型はすごいことになっている。target を指定してtarget型の値を返す。

Prelude> :module +Text.Regex.Posix
Prelude Text.Regex.Posix> :t (=~)
(=~) :: (Text.Regex.Base.RegexLike.RegexMaker
           Regex CompOption ExecOption source,
         Text.Regex.Base.RegexLike.RegexContext Regex source1 target) =>
        source1 -> source -> target
  • Bool 型を指定するとBool 型を返す
> "my left foot" =~ "foo" :: Bool    -- > True
> "my left foot" =~ "^my" :: Bool    -- > True
> "my left foot" =~ "^my123" :: Bool -- > False
> "" =~ "^$" :: Bool                 -- > True

Int 型を指定するとマッチした回数を返す。


Prelude Text.Regex.Posix> "a star called henry" =~ "planet" :: Int
0
Prelude Text.Regex.Posix> "a star called henry" =~ "star" :: Int
1
Prelude Text.Regex.Posix> "a star called henry eeeeee" =~ "e" :: Int
8
Prelude Text.Regex.Posix> "honorificabilitudinitatibus" =~ "[aeiou]" :: Int
13

String 型を指定するとStringを返す


Prelude Text.Regex.Posix> "honorificabilitudinitatibus" =~ "[aeiou]" :: String
"o"
Prelude Text.Regex.Posix> "honorificabilitudinitatibus" =~ "xyz" :: String
""
Prelude Text.Regex.Posix> "a star called henry" =~ "a star" :: String
"a star"

Prelude Text.Regex.Posix> "honorificabilitudinitatibus" =~ "[aeiou]" :: [String]
["o","o","i","i","a","i","i","u","i","i","a","i","u"]
Prelude Text.Regex.Posix> "a star called henry" =~ "a star" :: [String]
["a star"]

型を指定しないとエラー

 
Prelude Text.Regex.Posix> "honorificabilitudinitatibus" =~ "[aeiou]"

<interactive>:1:0:
No instance for (Text.Regex.Base.RegexLike.RegexContext
Regex [Char] target)
arising from a use of `=~' at <interactive>:1:0-41
Possible fix:
add an instance declaration for
(Text.Regex.Base.RegexLike.RegexContext Regex [Char] target)
In the expression: "honorificabilitudinitatibus" =~ "[aeiou]"
In the definition of `it':
it = "honorificabilitudinitatibus" =~ "[aeiou]"
id:kazu-yamamoto 指摘ありがとうございます。私の書いた[String]と言う記述だとエラーになるようです。何故、[String]としたのか今となっては不明です。(2012/02/17)

>  "honorificabilitudinitatibus" =~ "[aeiou]" :: String
-- > "o"

>  "honorificabilitudinitatibus" =~ "[aeiou]" :: [String]

<interactive>:1:31:
    No instance for (RegexContext Regex [Char] [String])
      arising from a use of `=~'
    Possible fix:
      add an instance declaration for
      (RegexContext Regex [Char] [String])
    In the expression:
          "honorificabilitudinitatibus" =~ "[aeiou]" :: [String]
    In an equation for `it':
        it = "honorificabilitudinitatibus" =~ "[aeiou]" :: [String]

>  "honorificabilitudinitatibus" =~ "[aeiou]" :: [[String]]
-- > [["o"],["o"],["i"],["i"],["a"],["i"],["i"],["u"],["i"],["i"],["a"],["i"],["u"]]

>  "a star called henry" =~ "a star" :: [String]

<interactive>:1:23:
    No instance for (RegexContext Regex [Char] [String])
      arising from a use of `=~'
    Possible fix:
      add an instance declaration for
      (RegexContext Regex [Char] [String])
    In the expression: "a star called henry" =~ "a star" :: [String]
    In an equation for `it':
        it = "a star called henry" =~ "a star" :: [String]
>  "a star called henry" =~ "a star" :: [[String]]
-- > [["a star"]]