首页 > 解决方案 > 替换后删除尾部斜杠

问题描述

我需要[/]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}\/)+$)

比赛有:

这让我很困惑...

有任何想法吗?

标签: javaregex

解决方案


替代方案:

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

推荐阅读