In this article, we are going to see how can we extract information from a Exception/Trace messages using Regular Expression.
This is regular basis need, spatially when you are analyzing a trace message that you found after an automated test process or unit test.
For basic idea about regular expression, see this old post.
Java Exception Format
When analyzing a trace message, we have to retrieve information from a single trace message from trace stack. A sample example (android trace message)
at widget.List.fillFrom(ListView.java:709)
In here, the format of the trace is packageName.className.methodName(FileName:lineNumber)
Retrieve with analysis
So, by construction all type of names that are used in java are made of alphabets, so the expression of every name will be [a-zA-Z]+.
(any character, single of multiple which are under rage of a-z, A-Z)
Every line number may have single character to 4/5 character(if we write very long class). So , for line number the expression will be \d+
(any digit more than 1 time)
Now, let’s look at the message:
- it begins with
at
- and have a space then the original message.
- And in the end it closes with
)
.
And after every name there is a Character(dot character or braces) .
So if we add those conditions together, we get these at\s[a-zA-Z]+.[a-zA-Z]+.[a-zA-Z]+.[a-zA-Z]+.[a-zA-Z]+:\d+\)$
Now we group our necessary items that we need to get from the expression. As we want package name, class name, method name, file name, line number: at\s([a-zA-Z]+).([a-zA-Z]+).([a-zA-Z]+).([a-zA-Z]+.[a-zA-Z]+):(\d+)\)$
If we do not want package name and class name , we just omit the braces: at\s[a-zA-Z]+.[a-zA-Z]+.([a-zA-Z]+).([a-zA-Z]+.[a-zA-Z]+):(\d+)\)$
Thanks …:)