scjp测试中的正则表达式测试 [英] Regex test in scjp test

查看:52
本文介绍了scjp测试中的正则表达式测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决有关正则表达式的scjp测试。

I am trying to solve an scjp test about regex.

这是一个代码......

here is a code...

import java.util.regex.*;

public class TestRegex {
    public static void main(String[] args) {
        Pattern p = Pattern.compile(args[0]);
        Matcher m = p.matcher(args[1]);
        boolean b = false;
        while (b = m.find()) {
            System.out.print(m.start() + m.group());
        }
    }
} 

java TestRegex "\d*" ab34ef 

此测试的答案是 01234456 。我理解除了最后一个输出(6)之外的所有内容。由于ab34ef中的最后一个索引是5,如何打印6?

the answer for this test is 01234456. I understood everything except the last output(6). Since the last index in "ab34ef" is 5, how is it possible to be printed 6 ?

任何帮助....

推荐答案

\d * 表示零个或多个数字,实际上可以匹配任何内容。 6是字符串中最后一个字符后面的空字符串匹配。

\d* means "zero or more digits," which can actually match nothing. The 6 is a match against the empty string after the last character in the string.

这篇关于scjp测试中的正则表达式测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆