0
votes

When reading up on functional programming I've stumbled upon the concept of Higher Order Functions which are, as far as I understand, functions that take functions as arguments and/or return functions as their result.

Is there an equivalent to this in the world of Regular Expressions? A Regex that operates on a Regex as to return a different Regex?

A simple (but useless) example would be:

(\w)+(?=\(er\|re\))

matches against

cent(er|re)

replacing the match with

calib

resulting in the Regex

calib(er|re)

Has anyone see this used anywhere? Can anyone think of a situation where this would be useful?

1

1 Answers

2
votes

I personally have not, and really can't think of a great application off there top of my head. I think the important thing to keep in mind with regex is the idea of Turing complete. Functional programming languages are all turing complete and thus functional programming is simply another way to logically order code. Granted there are plenty of argument to be made about the merits and benefits and scalability of function programming.

Regex, given that it is really nothing more than a text matching syntax, gains no real benefit from a functional style. This is why HTML is parsed using turing complete language parsers over a regex which would be arguably simpler to write (if it were possible).