Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i escape a group of special characters in java in one method?

i use lucene search but lucene has a bunch of special characters to escape like:

- && || ! ( ) { } [ ] ^ " ~ * ? : \

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!

like image 243
maccramers Avatar asked Feb 17 '12 06:02

maccramers


1 Answers

There is also a method called QueryParser#escape, which may be useful:

Returns a String where those characters that QueryParser expects to be escaped are escaped by a preceding \.

like image 106
Thilo Avatar answered Nov 04 '22 03:11

Thilo