Arsen Alexanyan Arsen Alexanyan - 1 year ago 57
Java Question

Match strings with regular expression in ignore case

I need to match strings in my array which are not starting with "KB" string.
I have tried this

String[] ar = {"KB_aaa","KB_BBB", "K_CCC", "!KBD", "kb_EEE", "FFFF"};
Pattern p = Pattern.compile("[^(^KB)].*");

for(String str : ar)
Matcher m = p.matcher(str);

But it still not matches "K_CCC".

Answer Source

A regex that matches anything not starting with KB is:


To do it in java:

if (str.matches("^(?!KB).*$")) ...