Regular expression combinator rationale - Factor Documentation (original) (raw)

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:

[ ] map

Without combinators, a naive approach would look as follows:

"|" join

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.