Regular expression combinator rationale
Regular expression combinators

Next:<literal> ( string -- regexp )


Regular expression combinators are useful when part of the regular expression contains user input. For example, given a sequence of strings on the stack, a regular expression which matches any one of them can be constructed:
[ <literal> ] map <or>

Without combinators, a naive approach would look as follows:
"|" join <regexp>

However, this code is incorrect, because one of the strings in the sequence might contain characters which have special meaning inside a regular expression. Combinators avoid this problem by building a regular expression syntax tree directly, without any parsing.