I’ve searched for a while how to use logical operation AND in regular expressions in Java, and have failed.
I’ve tried to do as recommended in similar topic:
(?=match this expression)(?=match this too)(?=oh, and this)
and it doesn’t work. Even simple examples with ?= returns false:
String b = "aaadcd";
System.out.println(b.matches("(?=aa.*)"));
Also I’ve read that (expression X)(expression Y) should work like X AND Y, but it works like X OR Y.
What am I doing wrong?
Added:
Tried to add .* in the end. Still don’t work.
For a example:
[2-9]?[0-9]{5,9}||1[2-9][0-9]{1,2}||120[0-9]{1,1}||119[0-9] = X – return false if number is less than 1190
[0-9]{1,3}||1[0-0][0-9]{1,2}||11[0-8][0-9]{1,1}||119[0-2] = Y – return false if number is greater than 1992.
String a = "1189";
a.matches(X) // return false
a.mathes(Y) // return true
a.matches((?=X)(?=Y).*) // return true, but should return false.
Added:
Yep, my regexp is not correct. My bad. The problem solved. Thank everyone very much!
I think what you need is
(?=X)Y(?=X)matches X, without consuming it (zero-width)Yand matches YThe main problem: X and Y are wrong, they should be (assuming 4 digits):
X:
119[0-9]|1[2-9][0-9]{2}|[2-9][0-9]{3}Y:
1[0-8][0-9]{2}|19[0-8][0-9]|199[0-2]Here a test code: