使用groovy读取IMAP消息内容字符串
您好,我正在开发我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我从 gmail 得到的字符串是这样的,
我尝试像这样处理它,
我希望现在已经清楚了。
我找到了答案。在我的正则表达式中,我搜索“\n”作为换行符,它确实适用于我伪造的用于测试解析的java字符串,但在来自gmail的电子邮件正文(纯文本/文本内容类型)中,我应该搜索“\r\n” ”。现在可以了!我太高兴了!
感谢你们所有的回复。我真的很感激。
and a string i get from gmail is something like this
i try to mach it like this
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.
它会起作用吗?
如果您将
WEB_SERVICE_REGEXP
更改为: 编辑,这是我所拥有的:
并且打印
Does it work if you change
WEB_SERVICE_REGEXP
to:edit
Here is what I have:
And that prints
更改:
至:
Change:
To: