• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extract Out Specific Parts Of A String

 
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I have a Java string 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 :-


"Resp Message - Component[Component Name] Server[Server Name] DataBase[Database Name] Status[Current Status]"

Now I would like to be able to extract out the 4 bits of information into seprate String for
each specific type eg

String component = "Component Name";
String server = "Server Name";
String database = "Database Name";
String status = "Current Status";

I would very much appreciate any help with how I can do this please.

Thank you.
Pete
 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String#split?
Regular expressions?

Before you can do anything, you must obtain a strict specification of the format of the String.
 
Pete Long
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ritchie

Thank you for the reply.

As regards to the String specification .. it will be as per my example ie

"Resp Message - Component[Component Name] Server[Server Name] DataBase[Database Name] Status[Current Status]"

The only thing that will vary is the content between [] for each of the items of interest. Below are further examples

// different messages for Server, Database and Status
"Resp Message - Component[Component Name] Server[Short Name] DataBase[Much Longer Database Name] Status[Long Current Status]"

// Component is an empty message
"Resp Message - Component[] Server[Server Name] DataBase[Database Name] Status[Current Status]"

I hope this is clear.

Yes Regular Expression and Pattern Matchers .. I have been looking at these but can't get my head round these.

Just would appreciate an example on how I can apply these to hopefully kick start me.

Thank you again.

Pete

 
Campbell Ritchie
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Pete Long
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ritchie

Will look into regular expression.

As you have mentioned, it is not a straight forward subject matter.

In the off chance, if you or any one else can help me further I would very much appreciate it.

Regards
Pete
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

Bill
Spend some time with the java.lang.String JavaDocs - very useful stuff in there
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Pete Long
Greenhorn
Posts: 19
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gents

Much appreciated all the suggestions.

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.

Regards
Pete
 
reply
    Bookmark Topic Watch Topic
  • New Topic