[Haskell-cafe] Annoucing alternative to Text.Regex
Bulat Ziganshin
bulat.ziganshin at gmail.com
Wed Mar 8 03:33:38 EST 2006
Hello Chris,
Wednesday, March 8, 2006, 1:26:37 AM, you wrote:
CK> It takes the string form of the regular expression and uses Parsec to create a
he-he, i written the same thing (but very simple) 2 years ago :) i
planned to submit it to the Parsec developers as an example of
double-Parsec usage :)
i think that it is a great lib, but not sure that it should completely
replace current lib. old lib is more appropriate for packed string,
new lib work directly with Haskell strings
one more interesting thing - generation of faster and simpler parsers
for simple regexps. just as example, code from my own program, that
parse filename wildcards. it translates simple patterns directly to
the "String->Bool" functions and use Regex library for more complex
patterns
-- |Compiled regexpr representation EXAMPLE
data RegExpr = RE_End -- ""
| RE_Anything -- "*"
| RE_FromEnd RegExpr -- '*':"bc"
| RE_AnyChar RegExpr -- '?':"bc"
| RE_Char Char RegExpr -- 'a':"bc"
| RE_FullRE Regex -- "r[0-9][0-9]"
is_wildcard s = s `contains_one_of` "?*["
translate_RE re = "^"++ (replaceAll "*" ".*"
.replaceAll "?" "."
.replaceAll "$" "\\$"
.replaceAll "[[[" "[^"
.replaceAll "^" "\\^"
.replaceAll "[^" "[[["
.replaceAll "+" "\\+"
.replaceAll "." "\\.") re ++"$"
compile_RE s = case s of
"" -> RE_End
"*" -> RE_Anything
'*':cs -> if ('*' `elem` cs) || ('[' `elem` cs)
then RE_FullRE (mkRegex$ translate_RE$ s)
else RE_FromEnd (compile_RE$ reverse$ s)
'[':cs -> RE_FullRE (mkRegex$ translate_RE$ s)
'?':cs -> RE_AnyChar (compile_RE cs)
c :cs -> RE_Char c (compile_RE cs)
match_RE re s = case re of
RE_End -> null s
RE_Anything -> True
RE_FullRE r -> isJust (matchRegex r s)
RE_FromEnd r -> match_RE r (reverse s)
RE_AnyChar r -> case s of
"" -> False
_:xs -> match_RE r xs
RE_Char c r -> case s of
"" -> False
x:xs -> x==c && match_RE r xs
match re {-s-} = match_RE (compile_RE re) {-s-}
{-# NOINLINE translate_RE #-}
--
Best regards,
Bulat mailto:Bulat.Ziganshin at gmail.com
More information about the Libraries
mailing list