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);
if(m.matches())
System.out.println(str);
}
But it still not matches "K_CCC". Thanks
The i modifier is used to perform case-insensitive matching. For example, the regular expression /The/gi means: uppercase letter T , followed by lowercase character h , followed by character e . And at the end of regular expression the i flag tells the regular expression engine to ignore the case.
IGNORECASE : This flag allows for case-insensitive matching of the Regular Expression with the given string i.e. expressions like [A-Z] will match lowercase letters, too. Generally, It's passed as an optional argument to re. compile() .
In Java, by default, the regular expression (regex) matching is case sensitive.
String[] ar = {"KB_aaa","KB_BBB", "K_CCC", "!KBD", "kb_EEE", "FFFF"};
Pattern p = Pattern.compile("^KB.*", Pattern.CASE_INSENSITIVE);
for(String str : ar)
{
Matcher m = p.matcher(str);
if(!m.matches())
System.out.println(str);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With