[英]How to ignore special characters ($ ^ + () {} etc.) from string, with the help of regex expression by using replaceAll() method
I am using java replaceAll() method to replace part of String with another String and its working great but, the problem comes when my file name contains characters like $ ^ + ( ) { } [ ] etc. In this case pattern matching fails and the original String remains as it is. 我使用java replaceAll()方法用另一个String替换部分String并且它工作得很好但是,当我的文件名包含像$ ^ +(){} []等字符时出现问题。在这种情况下,模式匹配失败,原始字符串保持不变。 Sample code to show case my use case is as follow:
示例代码示例我的用例如下:
String messageBody = "src=\"http://thinconnect.interactcrm.com:36061/FileDownloader/4/outbound/31358/file+name.jpeg\" style=\"height:225px\"";
messageBody = messageBody.replaceAll("(http|https)://(?:[^\\s]*)/FileDownloader/4/outbound/31358/file+name.jpeg", "cid: 14890411127853");
System.out.println(messageBody);
The expected output is: 预期的产出是:
src="cid: 14890411127853" style="height:225px"
src =“cid:14890411127853”style =“height:225px”
but it gives: 但它给出了:
src="http://thinconnect.interactcrm.com:36061/FileDownloader/4/outbound/31358/file+name.jpeg" style="height:225px"
src =“http://thinconnect.interactcrm.com:36061/FileDownloader/4/outbound/31358/file+name.jpeg”style =“height:225px”
How can I get it working by ignoring special characters that we use to form regex expression from my file name. 如何通过忽略我们用来从我的文件名形成正则表达式的特殊字符来使其工作。
Thanks in advance! 提前致谢!
You have unescaped metacharacters in your URL pattern, including a plus and a literal dot. 您的URL模式中有未转义的元字符,包括加号和文字点。 Escape them, using the following pattern:
使用以下模式逃脱它们:
(http|https)://(?:[^\\s]*)/FileDownloader/4/outbound/31358/file\\+name\\.jpeg
^^^ escape dot and plus sign
Full code: 完整代码:
String messageBody = "src=\"http://thinconnect.interactcrm.com:36061/FileDownloader/4/outbound/31358/file+name.jpeg\" style=\"height:225px\"";
messageBody = messageBody.replaceAll("(http|https)://(?:[^\\s]*)/FileDownloader/4/outbound/31358/file\\+name\\.jpeg", "cid: 14890411127853");
System.out.println(messageBody);
Output: 输出:
src="cid: 14890411127853" style="height:225px"
Update: 更新:
If you don't know in advance what the exact pattern will be, but you know it might have metacharacters, which would require escaping for use in a replacement, then Java provides a method for this: Pattern.quote() 如果您事先并不知道确切的模式是什么,但是您知道它可能有元字符,这需要转义才能用于替换,那么Java为此提供了一种方法: Pattern.quote()
To see how it works, we can split your pattern into two parts: 要了解它是如何工作的,我们可以将您的模式分为两部分:
String part1 = "(http|https)://(?:[^\\s]*)";
String part2 = Pattern.quote("/FileDownloader/4/outbound/31358/file+name.jpeg");
messageBody = messageBody.replaceAll(part1 + part2, "cid: 14890411127853");
From the documentation for Pattern.quote() : 从Pattern.quote()的文档:
This method produces a String that can be used to create a Pattern that would match the string s as if it were a literal pattern.
此方法生成一个String,该String可用于创建与字符串s匹配的Pattern,就好像它是文字模式一样。
Metacharacters or escape sequences in the input sequence will be given no special meaning.输入序列中的元字符或转义序列将没有特殊含义。
You just have to escape those characters using a backslash ( \\
) 你只需要使用反斜杠(
\\
)来逃避这些字符
example: 例:
String messageBody = "src=\"http://thinconnect.interactcrm.com:36061/FileDownloader/4/outbound/31358/file+name.jpeg\" style=\"height:225px\"";
messageBody = messageBody.replaceAll("(http|https)://(?:[^\\s]*)/FileDownloader/4/outbound/31358/file\\+name\\.jpeg", "cid: 14890411127853");
similarly 同样
String messageBody = "src=\"http://thinconnect.interactcrm.com:36061/FileDownloader/4/outbound/31358/file$name.jpeg\" style=\"height:225px\"";
messageBody = messageBody.replaceAll("(http|https)://(?:[^\\s]*)/FileDownloader/4/outbound/31358/file\\$name\\.jpeg", "cid: 14890411127853");
Did it this way. 这样做了吗?
final String[] metaCharacters = {"^","$","{","}","[","]","(",")",".","+","-","&"};
String filePath = "/4/outbound/31358/file+name.jpeg";
for(String c: metaCharacters){
if(filePath.contains(c)){
filePath = filePath.replace(c, "\\"+c);
}
}
String messageBody = "src=\"http://thinconnect.interactcrm.com:36061/FileDownloader/4/outbound/31358/file+name.jpeg\" style=\"height:225px\"";
System.out.println(messageBody);
messageBody = messageBody.replaceAll("(http|https)://(?:[^\\s]*)/FileDownloader"+filePath, "cid: 14890411127853");
System.out.println(messageBody);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.