如何用Java一口气从整个字符串中转义所有特殊字符 [英] How to escape all Special Characters from whole string at one go in Java
本文介绍了如何用Java一口气从整个字符串中转义所有特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Lucene支持转义查询语法中包含的特殊字符.当前列表中的特殊字符是
Lucene supports escaping special characters that are part of the query syntax. The current list special characters are
+ - && || ! ( ) { } [ ] ^ " ~ * ? : \
要转义这些字符,请在字符前使用\.例如,要搜索(1 + 1):2,请使用查询:
To escape these character use the \ before the character. For example to search for (1+1):2 use the query:
\(1\+1\)\:2
我的问题是如何一次逃逸整个字符串?例如myStringToEscape = "ABC^ " ~ * ? :DEF";
如何获取 escapedString .
推荐答案
You can use QueryParser.escape
, such as:
String escapedString = queryParser.escape(searchString);
queryParser.parse("field:" + escapedString);
这篇关于如何用Java一口气从整个字符串中转义所有特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文