Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a function remove which takes a regex as a string and another string. It removes anything that matches the regex from the second string and returns it.

At the moment, I'm calling the remove function with literal regex strings, for example:

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

This program is going to grow and there will be lots of regex's to call, and each one may be used several times throughout the program. Should I be storing them like this:

myregex = "(my|a)?string"

or should I be making a data type or something else?

Thanks

like image 495
Nick Brunt Avatar asked Jan 19 '23 12:01

Nick Brunt


2 Answers

One option would be to use partial application as in:

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

For each specific type of regex you want to apply, you can write a function like:

removeName = remove "<name_regex>"

and so on. Then you can use those functions like

removeName "myname"
like image 63
Ankur Avatar answered Jan 29 '23 23:01

Ankur


If performance is a concern, any regexen you intend to use multiple times should be compiled once and stored in compiled form. See also the documentation for makeRegex.

like image 42
Daniel Wagner Avatar answered Jan 30 '23 00:01

Daniel Wagner