regex - Java Pattern Matcher Woes -
i'm looking pattern check boolean terms in conditions:
- if exist them selves
- if not part of word or phrase
- terms or phrases ignore:
- string = "and";
- string b = "\tand";
- string c = "and ";
- string d = "this or that";
- string e = "band";
- string f = "l'or"
- string g = "can do";
code have far find them if have spaces before , after delimiter , sort of adjustment breaks progress have. i used page reference still no dice. have tried using both find() , matches() find seems broad in scope , matches doesn't seem broad enough. ideas?
final static pattern booleanterms = pattern.compile("(.*)(( or )|( or )|( not )|( not )( , )|( , ))(.*)"); public static void main(string[] args) { set<string> terms = new hashset<string>(); terms.add(" or"); //false terms.add("or "); //false terms.add("or"); // false terms.add(" or "); //true (string s : terms) { system.out.println(finddilims(s)); } // end loop } // end main method public static boolean finddilims(string s) { matcher matcher = booleanterms.matcher(s); if (matcher.matches()) { return true; } else { return false; } } // end method
you said want find them if , not part of phrase. don't want start , end pattern (.*)
.
it seems want find them if there whitespace around them. need start , end patter \s*
. want find them if there no space before or after. don't want have space on patterns, example in ( or )
.
and seems want case insensitive might want set (?i)
final static pattern booleanterms = pattern.compile("(?i)(\s*)((or)|(not)|(and))(\s*)");