Gavin
Gavin

Reputation: 4698

How to merge Regex?

Background

Let say I have several Regex here.

import Text.Regex

openTag = mkRegex "<([A-Z][A-Z0-9]*)\\b[^>]*>"
closeTag = mkRegex "</\\1>"
any = mkRegex "(.*?)"

Problem

openTag ++ any ++ closeTag <-- Just for illustration purpose

How can I merge them? To be specific, a Regex -> Regex -> Regex function. Alternatively, convert a Regex back to String would be good.

openTag ++ "hello" ++ closeTag <-- Just for illustration purpose

Thus, I can create my own Regex -> String -> Regex function ultimately.

Workaround

Manipulate the string literals.

import Text.Regex

openTag = "<([A-Z][A-Z0-9]*)\\b[^>]*>"
closeTag = "</\\1>"
any = "(.*?)"

tagWithAny = mkRegex $ openTag ++ any ++ closeTag

tagWith :: String -> Regex
tagWith s = mkRegex $ openTag ++ s ++ closeTag

Upvotes: 1

Views: 96

Answers (1)

phadej
phadej

Reputation: 12133

Regex type in the Text.Regex is essentially a C pointer:

data Regex = Regex (ForeignPtr CRegex) CompOption ExecOption

AFAIK there is no way to recover the string representation of the posix regex, after it has been compiled. regcomp 3 man page.

If you’d like to operate on regular expression algebraically, wrap then in your own type to postpone the compiling or use for example regex-applicative.

Upvotes: 8

Related Questions