Saturday, 14 September 2013

Java regex: how to get rid of redundant patterns from (X) (Y) (X and Y)

Java regex: how to get rid of redundant patterns from (X) (Y) (X and Y)

I am writing Java regex patterns and have run into a situation where I
expect X or X or both but no null.
So the following regex pattern works fine for this.
(X)|(Y)|(XY)
But the problem is that X and Y can be quite long and I have to write a
lot of this kind, which means a lot of redundancy and maintenance
headache.
(X)?(Y)? doesn't work because it can match null which I don't want.
Thanks for reading my posting and I will greatly appreciate your help.

No comments:

Post a Comment