user3780650
user3780650

Reputation: 27

Regex match skipping first match

So I have a certain log file:

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.

And I am trying to parse it. However, when I try my regex on a test, it works fine and matches what I want. http://regex101.com/r/sL4eR0/13 But when I try it out on code, it skips the first match and output all matches afterwards. I am not too sure why or how to fix it. Code: http://jsfiddle.net/j7rXu/6/

Upvotes: 1

Views: 669

Answers (2)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89584

The reason your pattern no more works is that your split/reverse/join all the lines before applying the pattern. Consequence, all the dates that are on separate lines are now before each record, but your pattern is designed to work with the original order. See how localcat looks like after the split/reverse/join:

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...

This is why you obtain all the records except the last with the date/time of previous record.

The good way is probably to extract all the data you want and only after to reverse the result array. An other possibility is to rewrite your pattern for this new order (see demo). You can choose to work line by line too.

Upvotes: 1

Daws
Daws

Reputation: 702

It looks like the call to regex.test(localcat) is eating the first match of the regex. If you remove this line prior to the loop calling regex.exec(localcat) it should include the first match in the loop.

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/>");
    }
}

The description for RegExp.prototype.test() states:

As with exec (or in combination with it), test called multiple times on the same global regular expression instance will advance past the previous match.

Upvotes: 1

Related Questions