Java最后一次出现regex直到字符串结尾

时间:2013-06-27 15:57:16

标签: java regex string

我需要一个正则表达式来获取最后一次出现的.java; [number]或java; NONE和字符串结尾之间的文本。

以下是我输入的文字示例:

user: ilian
branch: HEAD
changed files:
FlatFilePortfolioImportController.java;1.78
ConvertibleBondParser.java;1.52
OptionKnockedOutException.java;1.1.2.1
RebatePayoff.java;NONE

possible dead-lock. The suggested solution is to first create a TransactionContext and then lock AccountableDataFactory.IMPORT_LOCK and PositionManagerSQL

基本上我需要在提交结束时获取注释,这是在最后一次更改的文件之后,可能以1.52,1.1.2.1或NONE结尾。

3 个答案:

答案 0 :(得分:0)

String regex = "\\.java;\\d+\\.\\d+(.+)";
Pattern p = Pattern.compile(regex, Pattern.DOTALL);
Matcher m = p.matcher(input);

if (m.find()) {
    System.out.println(m.group(1));
}

答案 1 :(得分:0)

<强>被修改 解决方案假设输入是单行(原始帖子中的行仅为了清楚起见,请参阅下面的OP注释)。

String input = "user: ilian branch: "
        + "HEAD changed files: "
        + "FlatFilePortfolioImportController.java;1.78 "
        + "ConvertibleBondParser.java;1.52 "
        + "possible dead-lock. The suggested solution is to first create a "
        + "TransactionContext and then lock AccountableDataFactory.IMPORT_LOCK "
        + "and PositionManagerSQL";
// checks last occurrence of java;x.xx, optional space(s), anything until end of input
Pattern pattern = Pattern.compile(".+java;[\\d\\.]+\\s+?(.+?)$");
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
    System.out.println(matcher.group(1));
}

输出:

possible dead-lock. The suggested solution is to first create a TransactionContext and then lock AccountableDataFactory.IMPORT_LOCK and PositionManagerSQL

答案 2 :(得分:0)

String comment = mydata.replaceAll("(?s).*java;[0-9,.]+|.*java;NONE", "");
System.out.println(comment);

适用于所有文件结尾并正确打印。