【问题标题】:Java: Get rid of a Character followed by a 1 or 2 Digit Number (0-15) using Regular ExpressionsJava:使用正则表达式摆脱一个字符后跟一个 1 或 2 位数字 (0-15)
【发布时间】:2015-02-28 17:51:04
【问题描述】:

我想从某些字符串中删除一个字符后跟一个数字 (0-15) 的所有序列。

我不是很喜欢正则表达式,我尽力了,但没有想出解决这个问题的办法。

对于数字序列,我使用了:http://utilitymill.com/utility/Regex_For_Range

表示序列的字符我使用“@”

需要做以下替换:

  • @12Test --> 测试(替换@12)
  • @0Test --> 测试(替换@0)
  • @16Test --> @16Test(@16 没有被替换,只有 0-15)

为了测试正则表达式,我创建了以下 JUnit 测试用例:

public class ReplacementTests {
@Test
public void testNoReplacement1() {
    String actual = "Should nothing happen with this String";
    String expected = "Should nothing happen with this String";
    actual = appendRegexReplacement(actual);
    assertEquals(expected, actual);
}

@Test
public void testNoReplacement2() {
    String actual = "12Should 5nothing 16happen2 with this13 String";
    String expected = "12Should 5nothing 16happen2 with this13 String";
    actual = appendRegexReplacement(actual);
    assertEquals(expected, actual);
}

@Test
public void testReplacement() {
    String actual = "@12There @144are @5some @16which i @15want to @0get rid of!";
    String expected = "There @144are some @16which i want to get rid of!";
    actual = appendRegexReplacement(actual);
    assertEquals(expected, actual);
}

private String appendRegexReplacement(String replacement) {
    String regex = "/^@.([0-9]|1[0-5])/";
    return replacement.replaceAll(regex, "");
}

}

前两个测试按预期运行。 第三个测试(实际上需要进行替换)结果如下:

  • 预期:@144 有一些 @16 我想摆脱它!
  • 实际:@12There @144are @5some @16which i @15want to @0get 摆脱!

在此先感谢,感谢您的帮助!

【问题讨论】:

    标签: java regex replace expression


    【解决方案1】:

    在 Java 中,您不能用/ 包围正则表达式。此外,. 需要在@ 和第一个数字之间有一个字符。最后,^ 只会匹配字符串的开头。因此,

    String regex = "/^@.([0-9]|1[0-5])/";
    

    应该是

    String regex = "@([0-9]|1[0-5])";
    

    最后,为了防止匹配三位数、四位数等序列,添加负前瞻:

    String regex = "@([0-9]|1[0-5])(?![0-9])";
    

    【讨论】:

      【解决方案2】:
      @(?:\d|1[0-5])(?!\d)
      

      或者对于java

      @(?:\\d|1[0-5])(?!\\d)
      

      试试这个。查看演示。替换为empty string

      https://www.regex101.com/r/fG5pZ8/9

      https://www.regex101.com/r/fG5pZ8/10

      【讨论】:

        【解决方案3】:

        你可以像这样使用正则表达式:

        public static void main(String a[]) {
            String s1 = "@01Test";
            String s2 = "@10Test";
            String s3 = "@16Test";
            String s4 = "@15Test";
            System.out.println(s1.replaceAll("@(0[0-9]|1[0-5])(?=Test)", ""));
            System.out.println(s2.replaceAll("@(0[0-9]|1[0-5])(?=Test)", ""));
            System.out.println(s3.replaceAll("@(0[0-9]|1[0-5])(?=Test)", ""));
            System.out.println(s4.replaceAll("@(0[0-9]|1[0-5])(?=Test)", ""));
        }
        

        O/P:

        Test
        Test
        @16Test
        Test
        

        【讨论】:

          猜你喜欢
          • 2018-06-20
          • 2019-08-12
          • 1970-01-01
          • 1970-01-01
          • 2022-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多