Java 在字符串的特殊字符前添加转义“\”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32498432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add escape "\" in front of special character for a string
提问by Visahan
I have a simple SQL query where I check whether the query matches any of the fields I have. I'm using LIKE statement for this. One of my field can have special characters and so does the search query. So I'm looking for a solution where I need to an escape "\" in front of the special character.
我有一个简单的 SQL 查询,用于检查查询是否与我拥有的任何字段匹配。我为此使用了 LIKE 语句。我的字段之一可以有特殊字符,搜索查询也是如此。所以我正在寻找一个解决方案,我需要在特殊字符前面转义“\”。
query = "hello+Search}query"
I need the above to change to
我需要将以上更改为
query = "hello\+Search\}query"
Is there a simple way of doing this other than searching for each special character separately and adding the "\". Because if I don't have the escape character I will get the error message
除了单独搜索每个特殊字符并添加“\”之外,是否有一种简单的方法可以做到这一点。因为如果我没有转义字符,我会收到错误消息
java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
Thanks in advance
提前致谢
采纳答案by Laurentiu L.
Decide which special characters you want to escape and just call
决定要转义的特殊字符并调用
query.replace("}", "\}")
You may keep all special characters you allow in some array then iterate it and replace the occurrences as exemplified. This method replaces all regex meta characters.
您可以将允许的所有特殊字符保留在某个数组中,然后迭代它并替换出现的示例。此方法替换所有正则表达式元字符。
public String escapeMetaCharacters(String inputString){
final String[] metaCharacters = {"\","^","$","{","}","[","]","(",")",".","*","+","?","|","<",">","-","&","%"};
for (int i = 0 ; i < metaCharacters.length ; i++){
if(inputString.contains(metaCharacters[i])){
inputString = inputString.replace(metaCharacters[i],"\"+metaCharacters[i]);
}
}
return inputString;
}
You could use it as query=escapeMetaCharacters(query);
Don't think that any library you would find would do anything more than that. At best it defines a complete list of specialCharacters.
您可以将它用作query=escapeMetaCharacters(query);
不要认为您会找到的任何图书馆会做更多的事情。它充其量定义了一个完整的 specialCharacters 列表。
回答by Bathsheba
You need to use \\
to introduce a \
into a string literal; that is you need to escapethe \
. (A single backslash is used to introduce special characters into a string: e.g. \t
is a tab.)
您需要使用\\
将 a\
引入字符串文字;那就是你需要逃避的\
。(单个反斜杠用于将特殊字符引入字符串:例如\t
是制表符。)
query = "hello\\+Search\\}query"
is what you need.
query = "hello\\+Search\\}query"
是你所需要的。
回答by AMahajan
I had to do same thing in javascript. I came up with below solution. I think it might help someone.
我必须在 javascript 中做同样的事情。我想出了以下解决方案。我认为这可能会帮助某人。
function escapeSpecialCharacters(s){
let arr = s.split('');
arr = arr.map(function(d){
return d.replace(/[-\/\^$*+?.()|[\]{}]/g, '\'+d)
});
let reg = new RegExp(arr.join(''));
return reg;
}
let newstring = escapeSpecialCharacters("hello+Search}query");