You can also match case insensitive regexs and make it more readable by using the Pattern.CASE_INSENSITIVE constant like:
Pattern mypattern = Pattern.compile(MYREGEX, Pattern.CASE_INSENSITIVE);
Matcher mymatcher= mypattern.matcher(mystring);
Answer from Christian Vielma on Stack OverflowYou can also match case insensitive regexs and make it more readable by using the Pattern.CASE_INSENSITIVE constant like:
Pattern mypattern = Pattern.compile(MYREGEX, Pattern.CASE_INSENSITIVE);
Matcher mymatcher= mypattern.matcher(mystring);
RegexBuddy is telling me if you want to include it at the beginning, this is the correct syntax:
"(?i)\\b(\\w+)\\b(\\s+\\1)+\\b"
I wouldn't use RegEx for everything.
for(String str : ar)
{
if(!str.toUpperCase().startsWith("KB"))
System.out.println(str);
}
From the way your question is worded, I'm not entirely sure whether you want the match to be case insensitive or not. This regex:
(?i)[^k][^b].*
uses the flag (?i) to turn off case sensitivity, and should do want you want.
String.matches requires the entire string to match the pattern. As if the pattern has an implied "^...$".
Pattern ignore = Pattern.compile(".*" + Pattern.quote(prompt) + ".*",
Pattern.CASE_INSENSITIVE);
is for a find like match.
This could have been done with the original pattern as:
if (mIgn.find()) {
System.out.println("Found at position " + mIgn.start());
}
Matches return true if the whole string matches the given pattern. For this it prefix ur matcher with '^' and suffix with '$' sign and hence it is not going to look for a substring.
find() return true in case of substring matches also.
Have a look - Difference between matches() and find() in Java Regex