我需要从日志中收集信息,不幸的是这些信息没有放在一起,但是它们之间还有其他条目。
例如,我想知道谁是出生孩子的父母。日志看起来像
[Mar-27-2019 20:17:32]*** Started pregnancy for Bella Goth with Vladimir Goth.
[Mar-27-2019 20:17:32]*** Started adoption of Ninon Caron for Jacqueline Leduc and Don Lothario.
[Mar-27-2019 20:17:32]*** Started adoption of Emile François for Marion Boyer and Paolo Rocca.
[Mar-27-2019 20:17:32]Started 4 pregnancies
[Mar-27-2019 20:17:32]*** Started pet pregnancy for Josie with Bartholomiaou A. Bittlebun Senior.
[Mar-27-2019 20:17:32]*** Started pet pregnancy for Blue with Tempête Romeo.
[Mar-27-2019 20:17:32]Started 2 pet pregnancies
[Mar-27-2019 20:17:32]Checking for random marriage
(...)
[Mar-28-2019 09:54:54]Nancy Landgraab delivered 1 baby.
[Mar-28-2019 09:54:54] Female delivered:
[Mar-28-2019 09:54:54] * Zélie Landgraab
[Mar-28-2019 09:54:54]Nancy Landgraab delivered 1 baby.
[Mar-28-2019 09:54:54]Bella Goth delivered 1 baby.
[Mar-28-2019 09:54:54] Female delivered:
[Mar-28-2019 09:54:54] * Jessica Goth
[Mar-28-2019 09:54:54]Bella Goth delivered 1 baby.
所以我需要一起收集的是:
[Mar-27-2019 20:17:32]*** Started pregnancy for Bella Goth with Vladimir Goth.
[Mar-28-2019 09:54:54]Bella Goth delivered 1 baby.
[Mar-28-2019 09:54:54] Female delivered:
[Mar-28-2019 09:54:54] * Jessica Goth
[Mar-28-2019 09:54:54]Bella Goth delivered 1 baby.
有什么简单的方法可以在Java中做到这一点?
答案 0 :(得分:0)
例如,我们可以设计一些表达式来查看哪些单词可能在所需行中,例如:
^(?=.*(?:delivered|\*\*\*\s+Started\s+pregnancy)).*$
,然后我们收集这些行。
如果要浏览/简化/修改该表达式,请在this demo的右上角进行解释。
import java.util.regex.Matcher;
import java.util.regex.Pattern;
final String regex = "^(?=.*(?:delivered|\\*\\*\\*\\s+Started\\s+pregnancy)).*$";
final String string = "[Mar-27-2019 20:17:32]*** Started pregnancy for Bella Goth with Vladimir Goth.\n"
+ "[Mar-27-2019 20:17:32]*** Started adoption of Ninon Caron for Jacqueline Leduc and Don Lothario.\n"
+ "[Mar-27-2019 20:17:32]*** Started adoption of Emile François for Marion Boyer and Paolo Rocca.\n"
+ "[Mar-27-2019 20:17:32]Started 4 pregnancies\n"
+ "[Mar-27-2019 20:17:32]*** Started pet pregnancy for Josie with Bartholomiaou A. Bittlebun Senior.\n"
+ "[Mar-27-2019 20:17:32]*** Started pet pregnancy for Blue with Tempête Romeo.\n"
+ "[Mar-27-2019 20:17:32]Started 2 pet pregnancies\n"
+ "[Mar-27-2019 20:17:32]Checking for random marriage\n"
+ "(...)\n"
+ "[Mar-28-2019 09:54:54]Nancy Landgraab delivered 1 baby.\n"
+ "[Mar-28-2019 09:54:54] Female delivered:\n"
+ "[Mar-28-2019 09:54:54] * Zélie Landgraab\n"
+ "[Mar-28-2019 09:54:54]Nancy Landgraab delivered 1 baby.\n"
+ "[Mar-28-2019 09:54:54]Bella Goth delivered 1 baby.\n"
+ "[Mar-28-2019 09:54:54] Female delivered:\n"
+ "[Mar-28-2019 09:54:54] * Jessica Goth\n"
+ "[Mar-28-2019 09:54:54]Bella Goth delivered 1 baby.";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
jex.im可视化正则表达式: