简体   繁体   中英

Why doesn't this Java regex work?

I have a Java regular expression that captures stack exceptions from a string:

((?s).+(?:Exception|Error)[^\n]++(?:\s+at .++)+)

and it matches my input string:

FOO - org.omg.CORBA.MARSHAL: com.ibm.ws.pmi.server.DataDescriptor; IllegalAccessException  minor code: 4942F23E    
         at com.ibm.rmi.io.ValueHandlerImpl.readValue(ValueHandlerImpl.java:199)
         at com.ibm.rmi.iiop.CDRInputStream.read_value(CDRInputStream.java:1429)
         at com.ibm.rmi.io.ValueHandlerImpl.read_Array(ValueHandlerImpl.java:625)
         at com.ibm.rmi.io.ValueHandlerImpl.readValueInternal(ValueHandlerImpl.java:273)
         at com.ibm.rmi.io.ValueHandlerImpl.readValue(ValueHandlerImpl.java:189)
         at com.ibm.rmi.iiop.CDRInputStream.read_value(CDRInputStream.java:1429)
         at com.ibm.ejs.sm.beans._EJSRemoteStatelessPmiService_Tie._invoke(_EJSRemoteStatelessPmiService_Tie.java:613)
         at com.ibm.CORBA.iiop.ExtendedServerDelegate.dispatch(ExtendedServerDelegate.java:515)
         at com.ibm.CORBA.iiop.ORB.process(ORB.java:2377)
         at com.ibm.CORBA.iiop.OrbWorker.run(OrbWorker.java:186)
         at com.ibm.ejs.oa.pool.ThreadPool$PooledWorker.run(ThreadPool.java:104)
         at com.ibm.ws.util.CachedThread.run(ThreadPool.java:137)< newline here >

but if I expand the pattern to this:

FOO - ((?s).+(?:Exception|Error)[^\n]++(?:\s+at .++)+)\n

it no longer matches . Why is that?

Even if the string has indeed a newline at the end, it doesn't match because the final \\n is already matched by .++ (you're using (?s) option). As .++ is greedy possessive, it will match everything to the end of the string without backtracking so \\n will always fail.

It seems like the last expression group is capturing everything including the end of that string in its expression. So, adding \\n is not going to be found since it is already part of the earlier group.

So, to test use:

FOO - ((?s).+(?:Exception|Error)([^\\n]++)((?:\\s+at .++)+))

You will see the groups that are captured by it. And you will see that the last group in there includes everything including the EOL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM