Regex Pattern for phone series 97890x00yz where x,y,z should not have same values ?
Asked
Active
Viewed 60 times
-3
-
1possible duplicate of [Learning Regular Expressions](http://stackoverflow.com/questions/4736/learning-regular-expressions) – Javier Feb 20 '15 at 10:54
-
All other things except x,y and z are constant?. i.e, 97890 and 00 are constant or can they change? – TheLostMind Feb 20 '15 at 10:54
-
they are constant..only x,y,z can vary – gurtej singh Feb 22 '15 at 04:40
2 Answers
1
Something like this will work for you :
public static void main(String... strings) {
// String s = "97890x00yz";
String s = "9789010023"; // output true.
System.out.println(s.matches("97890(\\d)00(?!\\1)(\\d)(?!\\1|\\2)\\d"));
//String s = "9789010022"; same regex - output : false
}

TheLostMind
- 35,966
- 12
- 68
- 104
-
-
`()` captures everything inside it. SO you capture digits and `(?!)` is negative look-ahead.. It makes sure that the mentioned string doesn't follow..`\\1` refers to first captured group (digit) . `\\2` referes to second and so on. – TheLostMind Feb 23 '15 at 06:26
-
-
-