pomsky
pomsky copied to clipboard
Conditionals
Most regex engines support conditionals à là (?(?=condition)then|else) or (?P<foo>condition)? (?(foo)then|else). The syntax varies slightly between implementations.
A subset of this feature can be be made available in engines like JavaScript, Python and Rust that don't support conditionals, by negating the condition in the else branch. This only works for character classes and lookaround, because they can be negated.
The syntax I have in mind:
if (>> 'condition')
'branch 1'
else
'branch 2'
When targeting PCRE, this can compile to
(?(?=condition)branch 1|branch 2)
When targeting engines that don't support conditionals:
(?=condition)branch 1|(?!condition)branch 2
PCRE would also support the following, whereas JavaScript/Python/Rust can't:
Start (:person('To' | 'From') | 'Subject') ': '
(if ::person
[w]+ '@' [w]+ '.' ['a'-'z']+
else
[.]+
) End