0

Trying to NOT match a list of words but words are case insensitive

    Administrator --> not match
    aDmInIsTrAtOr --> not match
    Root          --> not match
    root          --> not match

but I want to accept anything else

    jsmith          --> match
    JSmith          --> match 
    Any_Text_Really --> match
    Any_meta_char_%$#!_and_12345 --> match

I can make the positive match for insensitive words

    ^(?i)administrator$|^(?i)root$

But fail to make it reversed ? Based on "(?:x) Matches 'x' but does NOT remember the match. Also known as NON-capturing parenthesis" I tried:

    ^(?:(?i)administrator)$

But that fails too. Any idea ?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
xavi
  • 109
  • 6

2 Answers2

1

You could use

^(?i)(?!administrator|root).+

See a demo on regex101.com.

Jan
  • 42,290
  • 8
  • 54
  • 79
0

Assuming you are using Java, you may just do a positive, case insensitive, match for administrator and root, and then negate from Java:

String input = "administrator";
if (!input.matches("(?i)administrator|root")) {
    System.out.println("MATCH");
}
else {
    System.out.println("NO MATCH");
}

Note that String#matches in Java by default matches the entire input, so the phrase root administrator would actually pass the above logic. If you instead want to detect these words anywhere in the input string, then my answer would have to be adjusted.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360