I have a Javastring which is composed of several bits of information that I need to extract out.
It has 4 bits of information - Component, Server, Database and Status.
The information of each is provided after the name and is enclosed in .
The  is to define the start and end of the message and not part of the message.
Below is an example :-
You should go through the whole regular expression tutorial; regular expressions are not easy. The technique depends on the format being defined strictly. For example, are you allowed spaces before and after [ or ]?
You can split on multiple word characters followed by a [, but beware because the square bracket is a metacharacter so you need to escape it probably as \\[ I think that would be "\\w\\[" as a regular expression.
You can look for successive indices of [ and look for substrings. You may have to remove the final ] similarly.
Since there are only 4 names spelled the same every time, I see no need for anything more complicated than the usual String library. Regex would be gross overkill.
To extract the content of X
1. find the startingAt = indexOf( "X" );
2. find the indexOf("[", startingAt )
3. walk the string from that point, accumulating characters until you hit "]"
4. trim() leading and trailing spaces
Spend some time with the java.lang.String JavaDocs - very useful stuff in there
William Brogden wrote:Since there are only 4 names spelled the same every time, I see no need for anything more complicated than the usual String library. Regex would be gross overkill.
I wouldn't call it overkill. It does, of course, depend on how much you already know about regular expressions. But once you do know them, the solution is simpler than the one that you've described. Even more so if you want to start adding error checks to see if the string does have the expected pattern. But of course, they aren't necessary.
As the requirement for me to extract out the various snippets of string from a well defined format and I don't have an in depth of Regex, I will going with Bill suggestion on this occasion.
Going forward, well I need to up skill on RegEx - actually re up skill because it was a topic I had to understand for my SCJP 1.5 ... yes that was a few years ago and so have had plenty of time to forget.