What's the best way to store regex strings in Haskell?

I have a remove function that takes a regular expression as a string and another string. It removes everything that matches the regular expression from the second line and returns it.

At the moment, I call the remove function with literal regular expressions, for example:

 remove "(my|a)?string" "Test string" -- returns "Test " 

This program will grow, and there will be many regular expressions to call, and each of them can be used several times throughout the program. Should I store them as follows:

 myregex = "(my|a)?string" 

or should I do a data type or something else?

thanks

+4
source share
2 answers

One option is to use a partial application:

 remove regex str = <generic code to remove the regex expression from string> 

For each specific type of regular expression that you want to apply, you can write a function such as:

 removeName = remove "<name_regex>" 

etc. Then you can use features like

 removeName "myname" 
+5
source

If performance is a problem, any regular expression that you intend to use multiple times needs to be compiled once and saved in compiled form. See also the documentation for makeRegex .

+4
source

All Articles