Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex matching only prefix or only suffix (XOR)

Tags:

java

regex

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)

like image 930
Rogue Avatar asked Mar 05 '16 16:03

Rogue


1 Answers

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

like image 128
fronthem Avatar answered Oct 08 '22 04:10

fronthem