匹配所有内容直到模式

时间:2015-01-29 14:15:25

标签: regex

我有以下正则表达式:

(?<time>[^ ]* [^ ]* ) (?<class>[^ ]+) *(?<level>[^ ]+)[ -]+(?<message>.*)

尝试匹配java堆栈跟踪,其示例为:

2015-01-28 18:48:33,484  grails.plugin.jms.JmsGrailsPlugin                   INFO   - registering listener for 'sendPostLinkByEmailToUnbindedContacts' of service 'postCreatedActivityListener' to TOPIC 'post.created'
2015-01-28 18:48:35,569  sf.ehcache.config.ConfigurationFactory              WARN   - No configuration found. Configuring ehcache from ehcache-failsafe.xml  found in the classpath: jar:file:/var/lib/tomcat7/webapps/ws/WEB-INF/lib/ehcache-core-2.4.8.jar!/ehcache-failsafe.xml
2015-01-28 18:48:37,809  proxy.pojo.javassist.JavassistLazyInitializer       ERROR  - HHH000142: Javassist Enhancement failed: hibe.core.communication.feed.Post
java.lang.RuntimeException: duplicate method: attach in hibe.core.communication.feed.Post_$$_javassist_64
        at javassist.util.proxy.ProxyFactory.createClass3(ProxyFactory.java:509)
        at ...

请参阅Rubular Link了解我的正则表达式和更完整的输入样本。

我的正则表达式正常,但message组除外,它与整个消息不匹配:我需要让message组包含整个堆栈跟踪直到下一个。知道下一个是否是这种格式的日期的模式:2015-01-28 18:48:33,484

可行吗?

1 个答案:

答案 0 :(得分:1)

使用:

  • DOTALL标志,因此dot也匹配换行符
  • 前瞻性消费,但不包括下一个日期戳
  • 不情愿的量词*?,因此匹配不会消耗到最后一个日期戳的所有输入

像这样:

(?<time>[^ ]* [^ ]* ) (?<class>[^ ]+) *(?<level>[^ ]+)[ -]+(?<message>.*?(?=\d{4}-\d\d-\d\d \d\d:\d\d:\d\d,\d{3}))

请参阅demo