如何以一种方法转义java中的一组特殊字符?
我使用 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 .
相关文章