2013-06-27 31 views
0

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

以下是文字的一个例子,我有作为输入:

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。

+0

如果您明确地说出示例输入中的输出应该是什么,那么问题会更加清晰。 –

+0

@JiriKremser我认为它从“可能的死锁”直到输入结束。我一开始也很困惑,不得不改变我的答案,因为我的OP意图提取版本号(!)... – Mena

回答

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

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

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)); 
} 
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 
+0

输出是在一行中,所以\ n不起作用。 我需要左边是类似.java;(十进制)的最后一次出现,之后出现<我想要的东西>,右侧应该是(输入结束),如果我是我没有错。 – Schadenfreude

+0

@ user2528840明白了。看我的编辑。 – Mena

相关问题