有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我需要什么正则表达式来读取这个值'12,'

if (thirdLastLine.matches("Tests run: ([1-9]|1[0-9]|2[0-3]),  Failures: ([1-9]|1[0-9]|2[0-3])")) {
            System.out.println("first choice");
            String[] splitedString = thirdLastLine.split("\\s");
            //  System.out.println("splitedString: " + splitedString);
            System.out.println("splitedString: " + Arrays.toString(splitedString));

        int lastIndexofArray = (splitedString.length) - 1;
        System.out.println("lastIndexOfArray: " + lastIndexofArray);

        //for the test run:
        int thirdIndexofArray = (splitedString.length) - 4;
        System.out.println("thirdIndexofArray: " + thirdIndexofArray);

        int returnedInteger = Integer.parseInt(splitedString[lastIndexofArray]);

       int returnedThirdInteger = Integer.parseInt(splitedString[thirdIndexofArray]);
        System.out.println("You failed: " + returnedInteger + " " + "test");
          System.out.println("Numbers of test:  " + returnedThirdInteger + "test");
        feedbackString = "You failed: " + returnedInteger + " " + "test";

    } else {
        System.out.println("Nothing found");

    }

thirdLastLine包含字符串:

Tests run: 12, Failures: 4

在txt文件中。故障位工作正常,但由于字符的原因,似乎无法使测试运行位工作。我无法读取“测试运行”旁边的值,因为它旁边有一个逗号,并且我的正则表达式不满足要求。有没有关于如何解决这个问题的建议


共 (5) 个答案

  1. # 1 楼答案

    您可以使用以下正则表达式来匹配数字表达式 2[0-3]|1\d|\d

    这将从0到23匹配

    如果你把它放在所有匹配模式中,并考虑到JE SUIS CHARLIE的答案

    它变成 "Tests run: (2[0-3]|1\d|\d), Failures: (2[0-3]|1\d|\d)"

  2. # 2 楼答案

      Pattern p = Pattern.compile("Tests run: ([1-9]|1[0-9]|2[0-3]),  Failures: ([1-9]|1[0-9]|2[0-3])");
        Matcher m = p.matcher(thirdLastLine);
        if (m.matches()) {
            String testsRun = m.group(1);
            String failedTests = m.group(2);
    
            int testsRunsOutput = Integer.parseInt(testsRun);
            int failedTestsOutput = Integer.parseInt(failedTests);
    
            System.out.println("You failed: " + failedTestsOutput + " " + "tests");
            System.out.println("Numbers of tests:  " + testsRunsOutput + "tests");
            System.out.println("Numbers of tests: " + testsRunsOutput + " " + "You failed: " + failedTestsOutput + " " + "tests");
            feedbackString = "Numbers of tests: " + testsRunsOutput + " " + "You failed: " + failedTestsOutput + " " + "tests";
    
        } else {
            System.out.println("Nothing found");
        }
    

    我刚刚使用了与我的问题相同的正则表达式和@Wil Selwood的代码。 当我运行带有“Tests run:12,Failures:4”的第三行时

    输出: 测试次数:12次失败:4次测试

  3. # 3 楼答案

    第一个数字后有一个逗号:

    "Tests run: ([1-9]|1[0-9]|2[0-3]),  Failures: ([1-9]|1[0-9]|2[0-3])"    
    #                         here __^
    

    在“故障”之前可能有未知数量的空格(或制表符),因此:

    "Tests run:\s+([1-9]|1[0-9]|2[0-3]),\s+Failures:\s+([1-9]|1[0-9]|2[0-3])"    
    
  4. # 4 楼答案

    如果我理解你的问题,我建议你用^{}得到^{},然后用^{} syntax之类的

    Pattern p = Pattern.compile("Tests run: (\\d+), Failures: (\\d+)");
    String thirdLastLine = "Tests run: 12, Failures: 4";
    Matcher m = p.matcher(thirdLastLine);
    if (m.matches()) {
        int testCount = Integer.parseInt(m.group(1));
        int failureCount = Integer.parseInt(m.group(2));
    
        System.out.printf("Numbers of tests: %d tests%n", testCount);
        String feedbackString = String.format("You failed: %d tests",
                failureCount);
        System.out.println(feedbackString);
    } else {
        System.out.println("Nothing found");
    }
    

    输出为

    Numbers of tests: 12 tests
    You failed: 4 tests
    
  5. # 5 楼答案

    你需要一个模式和匹配器来让小组出来

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    

    然后,您可以检查它是否匹配并提取匹配组

    Pattern p = Pattern.compile("Tests run: ([0-9]+), Failures: ([0-9]+)");
    Matcher m = p.matcher(thirdLastLine);
    if(m.matches()) {
       String testsRun = m.group(1);
       String failedTests = m.group(2);
       System.out.println("You failed: " + failedTests + " " + "tests");
       System.out.println("Numbers of tests:  " + testsRun + "tests");
    } else {
        System.out.println("Nothing found");
    }
    

    当然,您可能希望将结果转换为整数