問題描述
我使用 lucene 搜索,但 lucene 有一堆特殊字符可以轉義,例如:
i use lucene search but lucene has a bunch of special characters to escape like:
- && || ! ( ) { } [ ] ^ " ~ * ? :
我在轉義這些字符時遇到問題,因為它們太多了,如果我使用 String.replaceAll() 方法,我最終會得到很長的代碼行來轉義字符.最好的方法是什么?謝謝!
i am having problem escaping these characters because they are too many and if i use the String.replaceAll() method, i'll just end up having a really long line of code just for escaping the characters. what is the best way to do? thanks!
推薦答案
還有一個方法叫QueryParser#escape,這可能有用:
There is also a method called QueryParser#escape, which may be useful:
返回一個字符串,其中 QueryParser 期望被轉義的那些字符被前面的 轉義.
Returns a String where those characters that QueryParser expects to be escaped are escaped by a preceding .
這篇關于如何以一種方法轉義java中的一組特殊字符?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!