-6

我知道这种问题经常被提出,但我想不通为什么这个 RegEx 不匹配。我想检查行首是否有“M”。
最后,我想要行尾的路径。这就是为什么 startsWith() 不符合我的需求的原因。

line = "M      72208  70779 aab   src\com\aut\testproject\TestDomainf1.java";

if (line.matches("^(M?)(.*)$")) {}

我也尝试过另一种方法:

Pattern p = Pattern.compile("(M?)");
Matcher m = datePatt.matcher(line);
if (m.matches()) {
    System.out.println("yay!");
}

if (line.matches("(M?)(.*)")) {}

谢谢

4

1 回答 1

0

似乎很简单:

    if (line.startsWith("M")) {
        String[] tokens = line.split("\\s+");
        String path = tokens[tokens.length - 1];
    }
于 2013-08-19T13:45:21.970 回答