简体   繁体   English

如何通过使用replaceAll()方法在regex表达式的帮助下从字符串中忽略特殊字符($ ^ +(){}等)

[英]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.

相关问题 如何通过RegEx或replaceAll删除包含特殊字符的部分字符串? - How to remove part of string that includes special characters by RegEx or replaceAll? 忽略字符串中的特殊字符 - Ignore the special characters from the string 如何从字符串中删除多个特殊字符,而无需任何内置函数,例如下面的jdk 1.4的replaceAll() - How to remove multiple special characters from a String without any inbuilt functions like replaceAll() for jdk 1.4 below Java replaceAll()方法来转义特殊字符 - Java replaceAll() method to escape special characters 在Java中将replaceAll与包含特殊字符的字符串一起使用 - Using replaceAll with Strings containing special characters in Java Java 正则表达式 - 使用 String 的 replaceAll 方法替换换行符 - Java Regex - Using String's replaceAll method to replace newlines 如何忽略字符串中的特殊字符和空格? - How to ignore special characters and spaces in string? 如何使用java replaceAll(regex,replacement)方法屏蔽字符串中的最后一个括号? - How to mask last parentheses brackets in string using java replaceAll(regex, replacement) method? java string.replaceAll方法如何使用regex仅修剪右端? - How java string.replaceAll method works to trim only right end using regex? 使用String的ReplaceAll与正则表达式 - Using String's ReplaceAll with regex
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM