【问题标题】:Regex match skipping first match正则表达式匹配跳过第一场比赛
【发布时间】:2014-07-11 15:00:35
【问题描述】:

所以我有一个特定的日志文件:

Jul 07, 2014 12:56:06 AM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service Catalina
Jul 07, 2014 12:56:07 AM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [/admin] created a ThreadLocal with key of type [com.sun.xml.bind.v2.ClassFactory$1] (value [com.sun.xml.bind.v2.ClassFactory$1@75443fb0]) and a value of type [java.util.WeakHashMap] (value [{class com.hp.sw.bto.security.internal.jaxb.MultiValueMapAdapter=java.lang.ref.WeakReference@53b177f5, class com.hp.sw.bto.security.internal.PrincipleImpl=java.lang.ref.WeakReference@283aa0c0, class java.util.ArrayList=java.lang.ref.WeakReference@210fb1e2, class com.hp.sw.bto.security.internal.jaxb.MultivalueMapEntry=java.lang.ref.WeakReference@d677d63, class com.hp.sw.bto.security.context.builders.SecurityContextSectionBuilder$SecurityContextSectionImpl=java.lang.ref.WeakReference@558f575, class com.hp.sw.bto.security.authz.internal.AuthzPermissionImpl=java.lang.ref.WeakReference@135ad711, class com.hp.sw.bto.security.context.builders.GroupBuilder$GroupImpl=java.lang.ref.WeakReference@30dda704, class com.hp.sw.bto.security.context.builders.RoleBuilder$RoleImpl=java.lang.ref.WeakReference@280010ac, class com.hp.sw.bto.security.internal.jaxb.MultivalueMap=java.lang.ref.WeakReference@1c46a0b8, class com.hp.sw.bto.security.internal.SecurityContextImpl=java.lang.ref.WeakReference@60e19e88}]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
Jul 07, 2014 12:56:08 AM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [/admin] created a ThreadLocal with key of type [com.sun.xml.bind.v2.runtime.Coordinator$1] (value [com.sun.xml.bind.v2.runtime.Coordinator$1@7c6f2468]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@64bf67aa]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
Jul 07, 2014 12:56:09 AM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [/admin] created a ThreadLocal with key of type [com.sun.xml.bind.v2.ClassFactory$1] (value [com.sun.xml.bind.v2.ClassFactory$1@75443fb0]) and a value of type [java.util.WeakHashMap] (value [{class com.hp.sw.bto.security.internal.PrincipleImpl=java.lang.ref.WeakReference@522efd92, class java.util.ArrayList=java.lang.ref.WeakReference@b09a665, class com.hp.sw.bto.security.internal.jaxb.MultivalueMapEntry=java.lang.ref.WeakReference@268b368c, class com.hp.sw.bto.security.context.builders.SecurityContextSectionBuilder$SecurityContextSectionImpl=java.lang.ref.WeakReference@7daa3518, class com.hp.sw.bto.security.authz.internal.AuthzPermissionImpl=java.lang.ref.WeakReference@3183fb1c, class com.hp.sw.bto.security.context.builders.GroupBuilder$GroupImpl=java.lang.ref.WeakReference@4fdb04a9, class com.hp.sw.bto.security.context.builders.RoleBuilder$RoleImpl=java.lang.ref.WeakReference@340f1c34, class com.hp.sw.bto.security.internal.jaxb.MultivalueMap=java.lang.ref.WeakReference@4c04b49f, class com.hp.sw.bto.security.internal.SecurityContextImpl=java.lang.ref.WeakReference@48ee59b6}]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
Jul 07, 2014 12:56:10 AM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [/admin] created a ThreadLocal with key of type [com.sun.xml.bind.v2.runtime.Coordinator$1] (value [com.sun.xml.bind.v2.runtime.Coordinator$1@7c6f2468]) and a value of type [java.lang.Object[]] (value [[Ljava.lang.Object;@1843e122]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.

我正在尝试解析它。但是,当我在测试中尝试我的正则表达式时,它工作正常并且符合我想要的。 http://regex101.com/r/sL4eR0/13 但是当我在代码上尝试它时,它会跳过第一个匹配项,然后输出所有匹配项。我不太清楚为什么或如何解决它。 代码:http://jsfiddle.net/j7rXu/6/

【问题讨论】:

  • 仅供参考,regex101 模式在开始时缺少 ^,但是,当放置 ^ 时,它确实有效,所以你的问题成立。

标签: javascript regex parsing logging match


【解决方案1】:

看起来对regex.test(localcat) 的调用正在吃正则表达式的第一场比赛。如果您在循环调用 regex.exec(localcat) 之前删除此行,则它应该包含循环中的第一个匹配项。

var match, regex = /^([a-zA-Z]*): (.*)(?:\s)([^co]+) ([^\s]+) (\w*)$/gm, localcat = ((reader.result).split("\n").reverse().join("\n"));

// This line eats the first match of the regex
// alert(regex.test(localcat)); 

while ((match = regex.exec(localcat)) !== null){    
    alert(match[0]);

    if(match[1]==="SEVERE"){        
        document.getElementById("hey").innerHTML+= ("Time: " + match[3] + "<br/>");
    }
}

RegExp.prototype.test() 的 description 声明:

与 exec(或与它结合使用)一样,多次调用 test 在同一个全局正则表达式实例上将前进 上一场比赛。

【讨论】:

  • 非常感谢!你知道为什么会这样吗? @Daws
  • 你的解释很奇怪,不可能是真的,因为丢失的记录是文件中的第四条。
  • @user3780650,我相信 regex.test() 方法旨在与 exec() 方法结合使用,尽管我不完全确定为什么!请参阅developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… 处的说明。
  • @Daws:我不敢相信!我已经对其进行了测试,当您混合使用 testexec 方法时,我确实获得了您所描述的行为。极好的!但是,即使使用此修复程序,也总会丢失一条记录(而不是两条)。我相信原因就在我的回答中。
  • @CasimiretHippolyte:太好了,一开始我什至没有注意到最后一个条目被遗漏了。
【解决方案2】:

您的模式不再起作用的原因是您在应用模式之前拆分/反转/加入所有行。结果,单独行上的所有日期现在都在每条记录之前,但是您的模式旨在使用原始顺序。看看localcat在拆分/反向/加入后的样子:

Jul 07, 2014 12:56:06 AM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["http-nio-8080"]
Jul 07, 2014 12:56:06 AM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["http-bio-8443"]
Jul 07, 2014 12:56:06 AM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["ajp-apr-8009"]
Jul 07, 2014 12:56:06 AM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service Catalina
Jul 07, 2014 12:56:07 AM org.apache.catalina.loader.WebappClassLoader check...
SEVERE: The web application [/admin] created a ThreadLocal with key of type...
Jul 07, 2014 12:56:08 AM org.apache.catalina.loader.WebappClassLoader check...
SEVERE: The web application [/admin] created a ThreadLocal with key of type...
Jul 07, 2014 12:56:09 AM org.apache.catalina.loader.WebappClassLoader check...
SEVERE: The web application [/admin] created a ThreadLocal with key of type...
Jul 07, 2014 12:56:10 AM org.apache.catalina.loader.WebappClassLoader check...
SEVERE: The web application [/admin] created a ThreadLocal with key of type...

这就是为什么您会获取除最后一条记录之外的所有记录,以及上一条记录的日期/时间。

最好的方法可能是提取您想要的所有数据,然后再反转结果数组。另一种可能性是为这个新订单重写您的模式 (see demo)。您也可以选择逐行工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 2019-06-10
    相关资源
    最近更新 更多