我正在处理我的grails应用程序,我通过imap连接到gmail并等待收到的电子邮件。当电子邮件到达时,我需要获取其内容,并且它假设是一个字符串。然后我需要将此字符串与正则表达式匹配以获取最有价值的数据。问题是这个字符串有问题,它与我在通常的多行groovy字符串上测试的正则表达式不匹配,一切正常。
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()
}
我希望现在有点清楚。
答案 0 :(得分:1)
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 ”。它现在有效!我很开心!
感谢所有回复人员。对此,我真的非常感激。
答案 1 :(得分:0)
如果您将WEB_SERVICE_REGEXP
更改为:
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\/\.]*)$/
修改强>
这就是我所拥有的:
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
答案 2 :(得分:-1)
变化:
static def matches(String str, def regexp){
def m = str =~ regexp
m.find()
}
要:
static def matches(String str, regexp){
(str =~ regexp)
}