我使用此代码提取消息的 ID 值并区分消息类型。我使用的代码是这样的:
String lead = "You have received 1 lead\ntitle:my lead\nlead id:12345-2365\nalert setting id:890";
String reply = "You have received 1 reply\ntitle:my reply\nreply id:12345-2365\nad id:890\n";
Pattern leadPattern = Pattern.compile("lead id:\\s*(\\d{5}-\\d{4})", Pattern.MULTILINE);
Matcher leadMatcher = leadPattern.matcher(lead);
if (leadMatcher.find())
{
System.out.println("Lead: " + leadMatcher.group(1));
}
Pattern replyPattern = Pattern.compile("reply id:\\s*(\\d{5}-\\d{4})", Pattern.MULTILINE);
Matcher replyMatcher = replyPattern.matcher(reply);
if (replyMatcher.find())
{
System.out.println("Reply: " + replyMatcher.group(1));
}
System.out.println(leadPattern.matcher(reply).find());
System.out.println(replyPattern.matcher(lead).find());
它产生了以下结果:
Lead: 12345-2365
Reply: 1234-2365
false
false