user600838
user600838

Reputation:

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?

Upvotes: 1

Views: 141

Answers (2)

Newbo.O
Newbo.O

Reputation: 1948

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.

Upvotes: 1

gbvb
gbvb

Reputation: 886

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.

Upvotes: 0

Related Questions