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)
Is there a way of accomplishing this without having to duplicate the pattern?
The answer is "NO". Unlike and
and or
which are fundamental properties of regular expression. In regular expression, you can easily construct and
expression by using concatenation
and construct or
expression by using |
respectively.
But anyway, if you still want get your job done I suggest you to do this.
First, you already had two patterns here
\*\sZ
and
Z\:
So, as you said, these two patterns could not be occurred at the same time.
So from properties of xor
:
A xor B = (A & ~B)|(~A & B).
Finally, we can get
\*\sZ(?!\:)|(?<!\*\s)Z\:
See a DEMO
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With