使用groovy读取IMAP消息内容字符串

发布于 2024-11-09 04:51:51 字数 2616 浏览 1 评论 0原文

您好,我正在开发我的 grails 应用程序,我通过 imap 连接到 gmail 并等待传入​​的电子邮件。当电子邮件到达时,我需要获取其内容,它应该是一个字符串。然后我需要将此字符串与正则表达式匹配以获得最有价值的数据。问题是这个字符串有问题,它与我的正则表达式不匹配,我在通常的多行常规字符串上测试过,一切正常。

def processMessage(def imapMessage){
  if(message && message instanceof IMAPMessage){
              def content = message.getContent()
              println 'Content ='+ content
              m_mailContent = content
              println 'm_mailContent is ' + m_mailContent
          } 

然后

def getType(){
       def type = 0 
       if(utility.ParseUtil.matches(m_mailContent, WEB_SERVICE_REGEXP)){
          type = 1
       }else if(utility.ParseUtil.matches(m_mailContent, BNET_REGEXP)){
           type = 2
       }else if(utility.ParseUtil.matches(m_mailContent, QUEUED_REGEXP)){
           type = 3
       }
        return type
    }
But i almost always get type = 0

我可能需要对该字符串做一些事情。我不知道我得到什么样的字符串。打印时一切似乎都很好。当然,其中可能有一些特殊字符,但我要转义它们吗?

def WEB_SERVICE_REGEXP = /.*ErrorID:\s*[A-Za-z\d\/\.-]*\n.*Time:\s*[A-Za-z\d\/\s:]*\n.*URL:\s*[A-Za-z\d\/\.]*\n/

我从 gmail 得到的字符串是这样的,

ErrorID:  -732861900
Time:     Tue May 24 09:57:54 CEST 2011
URL:      /transnet/organisation.jsp
HttpCode: 500
Error:    class java.lang.SecurityException: Access denied.
User:     Peter Rådlund (peraa), DHL Express (Sweden) AB
Domain:   DHL Support

Stacktrace
----------------------------------------------------------------------
java.lang.SecurityException: Access denied.
       at com.memnon.transnet.admin.Account.checkUpdate(Account.java:725)
       at com.memnon.transnet.consignment.ConsignmentDB.updateOrganisation(ConsignmentDB.java:152)
       at org.apache.jsp.transnet.organisation_jsp._jspService(organisation_jsp.java:842)
       at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
       at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
       at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
       at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.A

我尝试像这样处理它,

//ParseUtil
static def matches(String str, def regexp){
            def m  = str =~ regexp
            m.find()
        }

我希望现在已经清楚了。

Hi i'm working on my grails application in wich i connect to gmail via imap and wait for incoming emails. When an email arrives i need to fetch its content and it suppose to be a string. Then i need to match this string to a regexp to get the most valuable data. The problem is that there is something wrong with this string and it doesn't match my regexp which i tested on usual multiline groovy string and everything worked fine.

def processMessage(def imapMessage){
  if(message && message instanceof IMAPMessage){
              def content = message.getContent()
              println 'Content ='+ content
              m_mailContent = content
              println 'm_mailContent is ' + m_mailContent
          } 

And then

def getType(){
       def type = 0 
       if(utility.ParseUtil.matches(m_mailContent, WEB_SERVICE_REGEXP)){
          type = 1
       }else if(utility.ParseUtil.matches(m_mailContent, BNET_REGEXP)){
           type = 2
       }else if(utility.ParseUtil.matches(m_mailContent, QUEUED_REGEXP)){
           type = 3
       }
        return type
    }
But i almost always get type = 0

I might need to do something to that string. I have no idea what kind of string i get. When print it everything seems fine. Of course it could be some special characters in it but do i escape them?

def WEB_SERVICE_REGEXP = /.*ErrorID:\s*[A-Za-z\d\/\.-]*\n.*Time:\s*[A-Za-z\d\/\s:]*\n.*URL:\s*[A-Za-z\d\/\.]*\n/

and a string i get from gmail is something like this

ErrorID:  -732861900
Time:     Tue May 24 09:57:54 CEST 2011
URL:      /transnet/organisation.jsp
HttpCode: 500
Error:    class java.lang.SecurityException: Access denied.
User:     Peter Rådlund (peraa), DHL Express (Sweden) AB
Domain:   DHL Support

Stacktrace
----------------------------------------------------------------------
java.lang.SecurityException: Access denied.
       at com.memnon.transnet.admin.Account.checkUpdate(Account.java:725)
       at com.memnon.transnet.consignment.ConsignmentDB.updateOrganisation(ConsignmentDB.java:152)
       at org.apache.jsp.transnet.organisation_jsp._jspService(organisation_jsp.java:842)
       at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
       at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
       at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
       at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.A

i try to mach it like this

//ParseUtil
static def matches(String str, def regexp){
            def m  = str =~ regexp
            m.find()
        }

I hope it's kind of clear now.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

面如桃花 2024-11-16 04:51:51
def WEB_SERVICE_REGEXP = /.*ErrorID:\s*[A-Za-z\d\/\.-]*\n.*Time:\s*[A-Za-z\d\/\s:]*\n.*URL:\s*[A-Za-z\d\/\.]*\n/

我从 gmail 得到的字符串是这样的,

ErrorID:  -732861900
Time:     Tue May 24 09:57:54 CEST 2011
URL:      /transnet/organisation.jsp
HttpCode: 500
Error:    class java.lang.SecurityException: Access denied.
User:     Peter Rådlund (peraa), DHL Express (Sweden) AB
Domain:   DHL Support

Stacktrace
----------------------------------------------------------------------
java.lang.SecurityException: Access denied.
       at com.memnon.transnet.admin.Account.checkUpdate(Account.java:725)
       at com.memnon.transnet.consignment.ConsignmentDB.updateOrganisation(ConsignmentDB.java:152)
       at org.apache.jsp.transnet.organisation_jsp._jspService(organisation_jsp.java:842)
       at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
       at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
       at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
       at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.A

我尝试像这样处理它,

//ParseUtil
static def matches(String str, def regexp){
            def m  = str =~ regexp
            m.find()
        }

我希望现在已经清楚了。

我找到了答案。在我的正则表达式中,我搜索“\n”作为换行符,它确实适用于我伪造的用于测试解析的java字符串,但在来自gmail的电子邮件正文(纯文本/文本内容类型)中,我应该搜索“\r\n” ”。现在可以了!我太高兴了!

感谢你们所有的回复。我真的很感激。

def WEB_SERVICE_REGEXP = /.*ErrorID:\s*[A-Za-z\d\/\.-]*\n.*Time:\s*[A-Za-z\d\/\s:]*\n.*URL:\s*[A-Za-z\d\/\.]*\n/

and a string i get from gmail is something like this

ErrorID:  -732861900
Time:     Tue May 24 09:57:54 CEST 2011
URL:      /transnet/organisation.jsp
HttpCode: 500
Error:    class java.lang.SecurityException: Access denied.
User:     Peter Rådlund (peraa), DHL Express (Sweden) AB
Domain:   DHL Support

Stacktrace
----------------------------------------------------------------------
java.lang.SecurityException: Access denied.
       at com.memnon.transnet.admin.Account.checkUpdate(Account.java:725)
       at com.memnon.transnet.consignment.ConsignmentDB.updateOrganisation(ConsignmentDB.java:152)
       at org.apache.jsp.transnet.organisation_jsp._jspService(organisation_jsp.java:842)
       at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
       at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
       at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
       at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.A

i try to mach it like this

//ParseUtil
static def matches(String str, def regexp){
            def m  = str =~ regexp
            m.find()
        }

I hope it's kind of clear now.

I found the answer. In my regex i search for "\n" as line breaker and it did work on java strings i faked to test parsing but in the email body from gmail(which is plain/text content type) i should search for "\r\n". It works now! I'm so happy!

Thanks for all of your responses guys. I really appreciate it.

夜还是长夜 2024-11-16 04:51:51

它会起作用吗?

def WEB_SERVICE_REGEXP = /(?m)^ErrorID:\s*([A-Za-z0-9\-]+)$[\n]^Time:\s*([A-Za-z\d\/\s:]*)$[\n]^URL:\s*([A-Za-z\d\/\.]*)$/

如果您将 WEB_SERVICE_REGEXP 更改为: 编辑

这是我所拥有的:

def message = '''
ErrorID:  -732861900
Time:     Tue May 24 09:57:54 CEST 2011
URL:      /transnet/organisation.jsp
HttpCode: 500
Error:    class java.lang.SecurityException: Access denied.
User:     Peter Rådlund (peraa), DHL Express (Sweden) AB
Domain:   DHL Support

Stacktrace
----------------------------------------------------------------------
java.lang.SecurityException: Access denied.
       at com.memnon.transnet.admin.Account.checkUpdate(Account.java:725)
       at com.memnon.transnet.consignment.ConsignmentDB.updateOrganisation(ConsignmentDB.java:152)
       at org.apache.jsp.transnet.organisation_jsp._jspService(organisation_jsp.java:842)
       at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
       at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
       at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
       at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.A'''


def WEB_SERVICE_REGEXP = /(?m)^ErrorID:\s*([A-Za-z0-9\-]+)$[\n]^Time:\s*([A-Za-z\d\/\s:]*)$[\n]^URL:\s*([A-Za-z\d\/\.]*)$/

static def matches( String str, def regexp ){
  def m  = str =~ regexp
  m.find()
}

println matches( message, WEB_SERVICE_REGEXP )

并且打印

true

Does it work if you change WEB_SERVICE_REGEXP to:

def WEB_SERVICE_REGEXP = /(?m)^ErrorID:\s*([A-Za-z0-9\-]+)$[\n]^Time:\s*([A-Za-z\d\/\s:]*)$[\n]^URL:\s*([A-Za-z\d\/\.]*)$/

edit

Here is what I have:

def message = '''
ErrorID:  -732861900
Time:     Tue May 24 09:57:54 CEST 2011
URL:      /transnet/organisation.jsp
HttpCode: 500
Error:    class java.lang.SecurityException: Access denied.
User:     Peter Rådlund (peraa), DHL Express (Sweden) AB
Domain:   DHL Support

Stacktrace
----------------------------------------------------------------------
java.lang.SecurityException: Access denied.
       at com.memnon.transnet.admin.Account.checkUpdate(Account.java:725)
       at com.memnon.transnet.consignment.ConsignmentDB.updateOrganisation(ConsignmentDB.java:152)
       at org.apache.jsp.transnet.organisation_jsp._jspService(organisation_jsp.java:842)
       at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
       at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:334)
       at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
       at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
       at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
       at org.apache.catalina.core.A'''


def WEB_SERVICE_REGEXP = /(?m)^ErrorID:\s*([A-Za-z0-9\-]+)$[\n]^Time:\s*([A-Za-z\d\/\s:]*)$[\n]^URL:\s*([A-Za-z\d\/\.]*)$/

static def matches( String str, def regexp ){
  def m  = str =~ regexp
  m.find()
}

println matches( message, WEB_SERVICE_REGEXP )

And that prints

true
祁梦 2024-11-16 04:51:51

更改:

static def matches(String str, def regexp){
            def m  = str =~ regexp
            m.find()
        }

至:

static def matches(String str, regexp){
       (str =~ regexp)
}

Change:

static def matches(String str, def regexp){
            def m  = str =~ regexp
            m.find()
        }

To:

static def matches(String str, regexp){
       (str =~ regexp)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文