【发布时间】:2019-04-09 14:42:04
【问题描述】:
需要有关正则表达式的帮助以检索错误堆栈中的电子邮件 ID,例如“一些文本,一些文本换行符等等”。
尝试使用堆栈溢出中提供的一些建议。但他们中的大多数只打印错误
以下是我尝试过的一些选项,
Matcher m = Pattern.compile("\\<([^>]+)\\)").matcher(e.getMessage());
while(m.find())
{
System.out.println(m.group(1));
}
System.out.println(e.getMessage().split("<(<^>>+)>"));
exception.getMessage().split("\\[([^]]+)\\]")
exception.getMessage().split("\\<\"(.*?)\"\\>")
exception.getMessage().split("<(<^>>+)>")
实际结果字符串数组只包含一个值并打印整个堆栈如下,
Failed messages: javax.mail.SendFailedException: Invalid Addresses;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.0.0 <abc@def.com>... User unknown
;
nested exception is:
com.sun.mail.smtp.SMTPAddressFailedException: 550 5.0.0 <def@ghi.com>... User unknown
;
PS:邮箱中没有\。堆栈溢出不接受
【问题讨论】:
-
您为什么要尝试使用拆分和正则表达式?拆分返回一个字符串数组,传递给它的参数就是它删除的参数。
-
@adickinson 实际上我对此很陌生。是的,使用 split 是不合适的。所以改为使用匹配器,它适用于不同的正则表达式。现在将发布答案。谢谢!