Match an expression with at least a prefix and/or a postfix without repeating the expressions

Viewed 1012

Let's say

((pre)(expr)(post)?|(pre)?(expr)(post))

This is meant to match expr with at least a prefix or a postfix or both but not with neither.

preexpr - OK
exprpost - OK
preexprpost - OK
expr - BAD

This example is using letters, but all three sub-expressions could be any valid regex, not just letters.

Is there a way of writing an equivalent regex with each expression appearing only once? I'm trying to improve readability.

Right now I know I can remove the second occurrence of pre:

((pre)(expr)(post)?|(expr)(post))

but I'm wondering if it can still be reduced.

Thank you

1 Answers
Related