开发者

Java String comparison / matching using boolean query (e.g. contains("('word1' AND 'word2') OR 'word3'") )

开发者 https://www.devze.com 2023-02-10 11:38 出处:网络
I don\'t really know how to exactly ask that questing but I hope the title says it already. I am searching for a way (a framework / lib) which provides an ability to do a String.contains() function w

I don't really know how to exactly ask that questing but I hope the title says it already.

I am searching for a way (a framework / lib) which provides an ability to do a String.contains() function which tells me if a given String matches a search query.

String1: "word1 word2 word3"
String2: 开发者_JAVA技巧"word3 word4 word5"

I want to ask:

String1.contains("('word1' AND 'word2') OR 'word3'")") ==> TRUE
String2.contains("('word1' AND 'word2') OR 'word3')") ==> TRUE
String2.contains("('word1' AND 'word2')")") ==> FALSE

Is there something out which does what I want? Things like Lucene search framework come to my mind but this seems overkill to what I want to achieve.

Thanks for any hints. If this question is already answered then sorry, I haven't found it.

Thanks Christoph


Lucene has indeed a BooleanQuery type of query that allows you to do just that. But if you really want to do something as simple as your examples, why don't use plain old boolean operators?

if ((string1.contains("word1") && string1.contains("word2")) 
   || string1.contains("word3") {
  // ...
}


The hamcrest matcher library could be useful, it lets you construct such constraints/predicates with any objects (not only strings). The newest version of Junit (4.x) uses this library to allow users to construct such predicate expressions.

http://code.google.com/p/hamcrest/

http://code.google.com/p/hamcrest/wiki/Tutorial

0

精彩评论

暂无评论...
验证码 换一张
取 消