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

查看:97
本文介绍了如何用一种方法在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!

推荐答案

还有一种名为

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天全站免登陆