Iamnino
Iamnino

Reputation: 380

Multiline RegEx read between two strings

I know this question has been asked many times and I assure you that I have read almost all posts about RegEx on the forum and tried for hours to get a solution. But finally this thing is killing me, slowly but surely! :D Any help is appreciated.

When I try to connect to a database and there is no db engine running, I get a connection refused Exception as expected. But for unknown reasons this exception is nested multiple (I don't know how many... 20, 30, 40...!!!) times and a call for myException.getCause() returns everytime NULL.

For that reason and because I don't want to show the whole stacktrace to users I thougt it would be nice and easy to catch just the main message with regular expressions. Unfortunately it is not as easy as I expected it to be.

First of all, do you think it is a good solution to strip down the message by cutting off the unwanted parts of a stacktrace? And if not, what do you suggest?

My Exception looks like that:

Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.ConnectException
MESSAGE: Connection refused: connect

STACKTRACE:

java.net.ConnectException: Connection refused: connect 
        at java.net.DualStackPlainSocketImpl.connect0(Native Method) 
        at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) 
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) 
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) 
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) 
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 
        at java.net.Socket.connect(Socket.java:589) 
        at java.net.Socket.connect(Socket.java:538) 
        at java.net.Socket.(Socket.java:434) 
        at java.net.Socket.(Socket.java:244) 
        at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:256) 
        at com.mysql.jdbc.MysqlIO.(MysqlIO.java:271) 
        at com.mysql.jdbc.Connection.createNewIO(Connection.java:2771) 
        at com.mysql.jdbc.Connection.(Connection.java:1555) 
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285) 
        at java.sql.DriverManager.getConnection(DriverManager.java:664) 
        at java.sql.DriverManager.getConnection(DriverManager.java:247)

............. and so on ............

I tried many different patterns to get the String between EXCEPTION ** and STACKTRACE for example:

EXCEPTION(\r?\n?.*?\r?\n?)*STACKTRACE

or

EXCEPTION\s*\W*(.*|\r*|\n*)+STACKTRACE

but nothing seems to work.

Thanks in advance

Upvotes: 1

Views: 1170

Answers (1)

Andreas
Andreas

Reputation: 159086

Try this: (?s)EXCEPTION \*\*\s*(.*?)\s*STACKTRACE.

In Java, that would be "(?s)EXCEPTION \\*\\*\\s*(.*?)\\s*STACKTRACE".

Or maybe without the two \s*, for better performance, and trim() the result.

See regex101 for demo.

Upvotes: 3

Related Questions