java - 替换后删除尾部斜杠
问题描述
我需要[/]00/
从给定的字符串中删除尾随。
例子:
string: '01/00/00/00/00/00/00/' -> 01
string: '01/02/03/00/00/00/00/' -> 01/02/03
string: '10/25/03/56/00/00/00/' -> 10/25/03/56
我一直在努力解决这个问题,但我不太清楚如何[/]
从[/]00
.
我的意思是,替换表达式必须避免以/
.
我试过这个表达式:
\d{2}\/(?=(?:\d{2}\/)+$)
比赛有:
这让我很困惑...
有任何想法吗?
解决方案
替代方案:
Pattern.compile("/00|/$", Pattern.MULTILINE).matcher(input).replaceAll("");
注意:当使用标志 Pattern.MULTILINE 时,将考虑边界匹配器“$”。
测试台和上下文中显示的正则表达式:
public static void main(String[] args) {
String input = "01/00/00/00/00/00/00/\n"
+ "01/02/03/00/00/00/00/\n"
+ "10/25/03/56/00/00/00/";
String result = Pattern.compile("/00|/$", Pattern.MULTILINE).matcher(input).replaceAll("");
System.out.println(result);
}
输出:
01
01/02/03
10/25/03/56
推荐阅读
- python-3.x - 行回计算或通过新的先前值更新行值
- spring - 找出 Spring Boot 应用程序在生产环境中速度变慢的最佳方法
- c++ - 在 Visual Studio 2017 中调试时如何回到崩溃线/线程?
- c# - IIS 10 是否为并发请求生成多个线程?如果是这样,它在什么基础上决定是否产生一个新线程?
- mongodb - MongoDB changeStream 初始值
- python - 如何同时在 Python tkinter 中读取 csv?
- javascript - 如何通过 aws-sdk 方法 listObjects 获取文件夹
- flask - Flask socket io .on('event') 未处理,但 .on('message') 工作正常
- google-sheets - 在列中搜索常用文本字符串
- aws-lambda - SNS 未触发 Lambda