Regex matching only prefix or only suffix (XOR)

Viewed 1749

I have a pattern, which I'll refer to as Z (actual pattern is a bit long and not important to the question). Simply put, I want to be able to match either \*\sZ, or Z\:, but not both nor neither.

I attempted using lookaheads (similar to below), however because of the pattern between the prefix and suffix they wouldn't work.

(\*\s(?!\:))Z((?<!\*)\:)

Is there a way of accomplishing this without having to duplicate the pattern (e.g. (\*\sZ|Z\:))?

A quick note about my pattern is there is no \* in the Z pattern, only in the prefix. Conversely there's also no \: in the Z pattern, it's only in the suffix if immediately proceeding Z, but not after any other characters (there's a .* capture after the suffix)

1 Answers
Related