2

I need to create a regex that returns true if the first three digits are not 666. Here are some examples:

66600abc - false

606asdfnh - true

600asdfasdf -true

I have tried this but I don't get the desired result.

System.out.println(Pattern.matches("[(^[(6)(6)(6)])][a-zA-Z0-9]*", "6660"));
System.out.println(Pattern.matches("[^6]{3}[a-zA-Z0-9]*", "606"));
System.out.println(Pattern.matches("[^666]{3}[a-zA-Z0-9]*", "506"));
System.out.println(Pattern.matches("[^666][a-zA-Z0-9]*", "636"));
System.out.println(Pattern.matches("[^666][a-zA-Z0-9]*", "666"));
sam1370
  • 335
  • 2
  • 18
Pravin Tukadiya
  • 489
  • 4
  • 20

3 Answers3

4

Use a negative lookahead (?!666) and to match alphanumeric symbols, you may use \p{Alnum}:

System.out.println("6660".matches("(?!666)\\p{Alnum}*"));

Note that matches() anchors the pattern by default, there is no need for ^ and $.

Some online tests:

System.out.println("6660".matches("(?!666)\\p{Alnum}*"));        // false
System.out.println("66600abc".matches("(?!666)\\p{Alnum}*"));    // false
System.out.println("606asdfnh".matches("(?!666)\\p{Alnum}*"));   // true
System.out.println("600asdfasdf".matches("(?!666)\\p{Alnum}*")); // true

UPDATE:

Since that is JFlex, you may use this regex if the minimum number of characters is 3 AND only alphanumeric symbols are allowed:

"^([a-zA-Z0-9]{1,2}$|[a-zA-Z0-57-9][a-zA-Z0-9]{2}|[a-zA-Z0-9][a-zA-Z0-57-9][a-zA-Z0-9]|[a-zA-Z0-9]{2}[a-zA-Z0-57-9])[a-zA-Z0-9]*"

If you need to allow any char, not just alphanumeric only, you may use replace [a-zA-Z0-9] with . and [a-zA-Z0-57-9] with [^6]:

"^(.{1,2}$|[^6].{2}|.[^6].|.{2}[^6]).*"

See the regex demo

NOTE that you may find a similar pattern in my previous Regex: match everything but SO answer.

Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Hello Wiktor I am using jFlex and ? mark is not support in it. – Pravin Tukadiya Oct 05 '16 at 09:39
  • Or just use `matches("666.*")` and add a `!` . i.e, `if (!.matches("666.*))` :P – TheLostMind Oct 05 '16 at 09:39
  • Try `"([a-zA-Z0-57-9]\\p{Alnum}{2}|\\p{Alnum}[a-zA-Z0-57-9]\\p{Alnum}|\\p{Alnum}{2}[a-zA-Z0-57-9])\\p{Alnum}*` (or [`([a-zA-Z0-57-9][a-zA-Z0-9]{2}|[a-zA-Z0-9][a-zA-Z0-57-9][a-zA-Z0-9]|[a-zA-Z0-9]{2}[a-zA-Z0-57-9])[a-zA-Z0-9]*`](https://regex101.com/r/xhXbJj/1)) - that will require at least 3 chars in the input string. Is there a minimum character threshold requirement? – Wiktor Stribiżew Oct 05 '16 at 09:44
3

Don't use Pattern.matches just to check a magic value.

If you want your string not begin with "666", just check with String.startWith(String):

class Test {
   public static Boolean isNotEvil(String str) {
     return !myString.startWith("666");
   }

  public static void main(String [] args)
  {
     System.out.println(Test.isNotEvil("azerty") ? "Not evil" : "Evil!" );
     System.out.println(Test.isNotEvil("66600abc") ? "Not evil" : "Evil!" );
     System.out.println(Test.isNotEvil("606asdfnh") ? "Not evil" : "Evil!" );
     System.out.println(Test.isNotEvil("600asdfasdf") ? "Not evil" : "Evil!" );
     System.out.println(Test.isNotEvil("666") ? "Not evil" : "Evil!" );
  }
}

Result:

 Not evil
 Evil!
 Not evil
 Not evil
 Evil!
ReaperSoon
  • 765
  • 7
  • 23
1

You can try this one:

([^6]|.[^6]|..[^6]).* 
olsli
  • 831
  • 6
  • 8