jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes Extract values from a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Extract values from a String" Watch "Extract values from a String" New topic

Extract values from a String

Mary Cole
Ranch Hand

Joined: Dec 02, 2000
Posts: 362

I have a String which is in the following format.

IS there a way in Java to extract the value after NAME: , VERSION: and CAT: using REGEX?

Thanks in advance
Kemal Sokolovic

Joined: Jun 19, 2010
Posts: 825

Yes. Check java.util.regex.Pattern and java.util.regex.Matcher classes.

The quieter you are, the more you are able to hear.
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 194

You can also achieve your goal without using regex. If this is the fixed format for the string just go and look for the end index of the keyword like NAME, VERSION etc. then find out the index of "]" just after this current index and fetch out the value in between the two indexes.

Do not wait to strike till the iron is hot; but make it hot by striking....
Winston Gutkowski

Joined: Mar 17, 2011
Posts: 8927

Mary Cole wrote:IS there a way in Java to extract the value after NAME: , VERSION: and CAT: using REGEX?

Yes, but first you think to think about what is the same and what is different about all those searches.

Regex is a pattern matcher, so you need to be looking for patterns, not specific Strings.


Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
Articles by Winston can be found here
I agree. Here's the link: http://aspose.com/file-tools
subject: Extract values from a String
It's not a secret anymore!