1

在 Java 中,以下正则表达式

To: a@b\.com.*Subject: Please verify your email address

不知何故在此文本中找不到匹配项:

Dez 21, 2012 10:29:58 AM com.google.appengine.api.datastore.dev.LocalDatastoreService init
INFO: Local Datastore initialized: 
    Type: High Replication
    Storage: In-memory
Dez 21, 2012 10:29:58 AM com.google.appengine.api.mail.dev.LocalMailService log
INFO: MailService.send
Dez 21, 2012 10:29:58 AM com.google.appengine.api.mail.dev.LocalMailService log
INFO:   From: 
Dez 21, 2012 10:29:58 AM com.google.appengine.api.mail.dev.LocalMailService log
INFO:   To: a@b.com
Dez 21, 2012 10:29:58 AM com.google.appengine.api.mail.dev.LocalMailService log
INFO:   Subject: Please verify your email address
Dez 21, 2012 10:29:58 AM com.google.appengine.api.mail.dev.LocalMailService log
INFO:   Body:
Dez 21, 2012 10:29:58 AM com.google.appengine.api.mail.dev.LocalMailService log
INFO:     Content-type: text/plain
Dez 21, 2012 10:29:58 AM com.google.appengine.api.mail.dev.LocalMailService log
INFO:     Data length: 4

我的 Java 代码如下所示:

    Matcher matcher = Pattern.compile(regex, Pattern.MULTILINE).matcher(input);
    if (matcher.find()) {
        ...
    }

这有点奇怪,因为当我使用此工具在线测试时,该模式似乎有效:http ://regexpal.com

因此,Java 必须对这种模式进行一些不同的解释。有什么办法可以得到错误信息Matcher吗?

更新它应该找到:

To: a@b.com
Dez 21, 2012 10:29:58 AM com.google.appengine.api.mail.dev.LocalMailService log
INFO:   Subject: Please verify your email address
4

1 回答 1

9

您将要使用Pattern.DOTALL而不是Pattern.MULTILINE.

DOTALL使.匹配换行符。(这就是你想要的)

MULTILINE在每行的基础上制作^和工作。$

于 2012-12-21T10:46:14.623 回答