简体   繁体   中英

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

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"

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM