How does one use Text.Regex.Base.RegexLike?

I'm trying to migrate code from using the old Text.Regex to the new Text.Regex.Base. But, I'm getting type errors. I can't even create a regex. Looking at the docs, it seems like this should print "bcd": import Data.Array import Text.Regex.Base import Text.Regex.Posix rx = makeRegex "a(.*)A" Just (_, mt, _) = matchOnceText rx "abcdA" main = putStrLn (fst (mt ! 0)) But I get an error: src\regex.hs:5:5: No instance for (RegexMaker regex compOpt execOpt [Char]) arising from a use of `makeRegex' at src\regex.hs:5:5-22 Possible fix: add an instance declaration for (RegexMaker regex compOpt execOpt [Char]) In the expression: makeRegex "a(.*)A" In the definition of `rx': rx = makeRegex "a(.*)A" src\regex.hs:7:18: No instance for (RegexLike regex [Char]) arising from a use of `matchOnceText' at src\regex.hs:7:18-41 Possible fix: add an instance declaration for (RegexLike regex [Char]) In the expression: matchOnceText rx "abcdA" In a pattern binding: Just (_, mt, _) = matchOnceText rx "abcdA" Why does it say there is no instance? Isn't the instance imported by Text.Regex.Posix? Why in the world is it so complicated just to get a matched substring out of the text? Is there an easier way? Thanks, Lyle

Hello, Does this help? http://www.serpentine.com/blog/2007/02/27/a-haskell-regular-expression-tutor... j. At Tue, 23 Dec 2008 11:21:41 -0800, Lyle Kopnicky wrote:
[1
] [1.1 ] I'm trying to migrate code from using the old Text.Regex to the new Text.Regex.Base. But, I'm getting type errors. I can't even create a regex. Looking at the docs, it seems like this should print "bcd": import Data.Array import Text.Regex.Base import Text.Regex.Posix
rx = makeRegex "a(.*)A"
Just (_, mt, _) = matchOnceText rx "abcdA"
main = putStrLn (fst (mt ! 0))
But I get an error:
src\regex.hs:5:5: No instance for (RegexMaker regex compOpt execOpt [Char]) arising from a use of `makeRegex' at src\regex.hs:5:5-22 Possible fix: add an instance declaration for (RegexMaker regex compOpt execOpt [Char]) In the expression: makeRegex "a(.*)A" In the definition of `rx': rx = makeRegex "a(.*)A"
src\regex.hs:7:18: No instance for (RegexLike regex [Char]) arising from a use of `matchOnceText' at src\regex.hs:7:18-41 Possible fix: add an instance declaration for (RegexLike regex [Char]) In the expression: matchOnceText rx "abcdA" In a pattern binding: Just (_, mt, _) = matchOnceText rx "abcdA"
Why does it say there is no instance? Isn't the instance imported by Text.Regex.Posix?
Why in the world is it so complicated just to get a matched substring out of the text? Is there an easier way?
Thanks, Lyle [1.2
] [2
] _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Yes, sort of. It enables me to get some simple examples working with (=~).
But I still don't know how to get makeRegex to work. You need it to specify
options like case insensitivity, or to use functions like matchAllText.
On Tue, Dec 23, 2008 at 11:29 AM, Jeremy Shaw
Hello,
Does this help?
http://www.serpentine.com/blog/2007/02/27/a-haskell-regular-expression-tutor...
j.
At Tue, 23 Dec 2008 11:21:41 -0800, Lyle Kopnicky wrote:
[1
] [1.1 ] I'm trying to migrate code from using the old Text.Regex to the new Text.Regex.Base. But, I'm getting type errors. I can't even create a regex.
Looking at the docs, it seems like this should print "bcd":
import Data.Array import Text.Regex.Base import Text.Regex.Posix
rx = makeRegex "a(.*)A"
Just (_, mt, _) = matchOnceText rx "abcdA"
main = putStrLn (fst (mt ! 0))
But I get an error:
src\regex.hs:5:5: No instance for (RegexMaker regex compOpt execOpt [Char]) arising from a use of `makeRegex' at src\regex.hs:5:5-22 Possible fix: add an instance declaration for (RegexMaker regex compOpt execOpt [Char]) In the expression: makeRegex "a(.*)A" In the definition of `rx': rx = makeRegex "a(.*)A"
src\regex.hs:7:18: No instance for (RegexLike regex [Char]) arising from a use of `matchOnceText' at src\regex.hs:7:18-41 Possible fix: add an instance declaration for (RegexLike regex [Char]) In the expression: matchOnceText rx "abcdA" In a pattern binding: Just (_, mt, _) = matchOnceText rx "abcdA"
Why does it say there is no instance? Isn't the instance imported by Text.Regex.Posix?
Why in the world is it so complicated just to get a matched substring out of the text? Is there an easier way?
Thanks, Lyle [1.2
] [2
] _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Am Dienstag, 23. Dezember 2008 21:09 schrieb Lyle Kopnicky:
Yes, sort of. It enables me to get some simple examples working with (=~). But I still don't know how to get makeRegex to work. You need it to specify options like case insensitivity, or to use functions like matchAllText.
Your problem is that GHC can't know which instance to choose. You can help it by giving a type signature, e.g. rx :: Regex rx = makeRegex "a(.*)A" HTH, Daniel
At Tue, 23 Dec 2008 11:21:41 -0800,
Lyle Kopnicky wrote:
I'm trying to migrate code from using the old Text.Regex to the new Text.Regex.Base. But, I'm getting type errors. I can't even create a
regex.
Looking at the docs, it seems like this should print "bcd":
import Data.Array import Text.Regex.Base import Text.Regex.Posix
rx = makeRegex "a(.*)A"
Just (_, mt, _) = matchOnceText rx "abcdA"
main = putStrLn (fst (mt ! 0))
But I get an error:
src\regex.hs:5:5: No instance for (RegexMaker regex compOpt execOpt [Char]) arising from a use of `makeRegex' at src\regex.hs:5:5-22 Possible fix: add an instance declaration for (RegexMaker regex compOpt execOpt [Char]) In the expression: makeRegex "a(.*)A" In the definition of `rx': rx = makeRegex "a(.*)A"
src\regex.hs:7:18: No instance for (RegexLike regex [Char]) arising from a use of `matchOnceText' at src\regex.hs:7:18-41 Possible fix: add an instance declaration for (RegexLike regex [Char]) In the expression: matchOnceText rx "abcdA" In a pattern binding: Just (_, mt, _) = matchOnceText rx "abcdA"
Why does it say there is no instance? Isn't the instance imported by Text.Regex.Posix?
Why in the world is it so complicated just to get a matched substring out
of
the text? Is there an easier way?
Thanks, Lyle [1.2
] [2
] _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Yes, that worked, thanks. I just figured that out too. Here's a whole
working program:
import Text.Regex.Base
import Text.Regex.Posix
rx :: Regex
rx = makeRegex "a(.*)A"
mr = match rx "abcdA"
text = head $ mrSubList mr
main = putStrLn text
On Tue, Dec 23, 2008 at 12:30 PM, Daniel Fischer
Am Dienstag, 23. Dezember 2008 21:09 schrieb Lyle Kopnicky:
Yes, sort of. It enables me to get some simple examples working with (=~). But I still don't know how to get makeRegex to work. You need it to specify options like case insensitivity, or to use functions like matchAllText.
Your problem is that GHC can't know which instance to choose. You can help it by giving a type signature, e.g.
rx :: Regex rx = makeRegex "a(.*)A"
HTH, Daniel

Yeah! I am glad the mailing list has helped. If you get stuck or need something fancier then let me know (I wrote RegexLike).
But I still don't know how to get makeRegex to work. You need it to specify options like case insensitivity, or to use functions like matchAllText.
Well, the options are defined in detail by the back end module, e.g. regex-posix. Take Text.Regex.Posix.Wrap: Most importantly is the "Regex" data type defined in this module. Also, there is an instance of "RegexMaker Regex CompOption ExecOption String". The CompOptions and ExecOptions are defined in this module, and can be passed to "makeRegexOpts" from the RegexMaker class that converts String/ByteString/.. to the Regex data type. The CompOptions are only settable at this compilation stage. Later you can use "getExecOpts" and "setExecOpts" to create a new Regex from the original Regex which differs in the value of ExecOptions it uses. These execution options can change after creating the Regex. The "makeRegex" uses the "defaultCompOpt" and "defaultExecOpt" values. The CompOptions for Text.Regex.Posix.Wrap are at http://www.haskell.org/ghc/docs/latest/html/libraries/regex-posix/Text-Regex... and are just a newtype of the underlying c-library's bitmapped int flags. AND I made this newtype derive the same "Bits" instance, so this works:
let r = makeRegexOpts (defaultCompOpts || compIgnoreCase) defaultExecOpts "caseInSensiTIVe"
By binding a Regex value like this and using it several times one saves the effort of compiling the String pattern into the Regex data type. If you need something that is not exactly given by the RegexLike API then there may be a RegexContext instance which does give you what you want. WARNING: The version of regex-base you are using matters most when it comes to the RegexContext instances. The latest 0.93.1 version is at http://hackage.haskell.org/packages/archive/regex-base/0.93.1/doc/html/Text-... and please note the "Source" link in the top right corner area which helps in understanding that RegexContext is just a convenience wrapper. ghc 6.10 is distributed with the 0.72.0.2 version which is at http://hackage.haskell.org/packages/archive/regex-base/0.72.0.2/doc/html/Tex... and again has a link the "Source". Hooray for hackage. -- Chris
participants (4)
-
ChrisK
-
Daniel Fischer
-
Jeremy Shaw
-
Lyle Kopnicky