简体   繁体   English

如何使用 replaceAll() 方法,如何指定我的正则表达式来删除任何黑斜线和字符?

[英]How to use the replaceAll() method, how can I specify my regex to delete any blackslash and char right after it?

In this string below, I need to remove the backslash and the char immediately after it.在下面的这个字符串中,我需要立即删除反斜杠和后面的字符。 Code snippet below下面的代码片段

String raw;
String s = "###\\A";)
String formatted = raw.replaceAll("\\.?", "");
System.out.println(formatted)

when I printed out the formatted, I got ###A instead of ### it appears that in the regex "\.?", the?当我打印出格式化后,我得到了###A 而不是### 它出现在正则表达式“\.?”中,? is not doing its job.没有做它的工作。 I may be missing something here.我可能在这里遗漏了一些东西。 My goal is to get rid of the // and the A. Any help will be appreciated我的目标是摆脱 // 和 A。任何帮助将不胜感激

Try this REGEX \\\\[AZ]?试试这个正则表达式\\\\[AZ]?

String s = "###\\ZABCDE";
String formatted = s.replaceAll("\\\\[A-Z]?", "");
System.out.println(formatted);

, output , output

###ABCDE

Regex Oracle doc 正则表达式 Oracle 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用反斜杠在 java replaceAll() 中反向引用正则表达式组 - how can I use backslash to backreference regex groups in java replaceAll() 如何在StringBuffer中使用replaceAll()方法? - How to use replaceAll() method in StringBuffer? java string.replaceAll方法如何使用regex仅修剪右端? - How java string.replaceAll method works to trim only right end using regex? 如何递归使用 replaceAll() function? - How do I use the replaceAll() function recursively? 如何使用 Java replaceAll() 方法在一种方法中使用多个字符串? - How can one use the Java replaceAll() method to using multiple strings in one method? 如何使用java String.replaceAll(字符串正则表达式,字符串替换)来获取我想要的? - How to use java String.replaceAll(string regex,string replacement) to get what I want? 如何在 Matcher.replaceAll() 中进行条件正则表达式替换? - How can one make a conditional regex replacement in Matcher.replaceAll()? 如何使用片段方法? - How can I use my fragment method? 如何在我的全局变量中存储一个值并在活动中的任何方法中使用它 - How can I store a value in my global variable and use it inside any method anywhere in activity ¿可以用*,$,%替换所有吗? 如果没有 ¿ - ¿Can replaceAll work with *,$,%? if no ¿What other alternative to replaceAll can i use?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM