如何以一种方法转义java中的一组特殊字符? [英] how can i escape a group of special characters in java in one method?

查看:13
本文介绍了如何以一种方法转义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 .

这篇关于如何以一种方法转义java中的一组特殊字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆