Solved

Java parser code to extract 2 distincts values from an String array

Posted on 2014-10-30
3
515 Views
Last Modified: 2014-10-30
Need to parse 2 values (tn and id) from a response String similar to this:

tn='447795584165' ptr {id=0}
0
Comment
Question by:yvan_vallee
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 40414790
Do you know regular expressions? If so, the below should hopefully make sense. If not, try it and see if it works, and if there are different format strings that need to be catered for, I can modify it for you.

package ee.yvanVallee;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExtractFields {
    
    public static void main(String[] args) {
        String testString = "tn='447795584165' ptr {id=0}";
        
        
        
        Pattern pattern = Pattern.compile("tn='([^']*)' ptr \\{id=([^}]*)\\}");
        
        Matcher matcher = pattern.matcher(testString);
        if (matcher.matches() && matcher.groupCount() >= 2) {
            String tn = matcher.group(1);
            String id = matcher.group(2);
            
            
            System.out.println("tn: " + tn);
            System.out.println("id: " + id);
        }
    }
    
}

Open in new window



Or if the values of both tn and id are always numeric (longs) only, you could do this...

package ee.yvanVallee;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ExtractFields {
    
    public static void main(String[] args) {
        String testString = "tn='447795584165' ptr {id=0}";
        
        
        
        Pattern pattern = Pattern.compile("tn='(\\d*)' ptr \\{id=(\\d*)\\}");
        
        Matcher matcher = pattern.matcher(testString);
        if (matcher.matches() && matcher.groupCount() >= 2) {
            long tn = new Long(matcher.group(1));
            long id = new Long(matcher.group(2));
            
            
            System.out.println("tn: " + tn);
            System.out.println("id: " + id);
        }
    }
    
}

Open in new window

0
 

Author Closing Comment

by:yvan_vallee
ID: 40414907
Hi mccarl,,
This is exactly what I need, already tested and working fine.

Thank you,
0
 
LVL 35

Expert Comment

by:mccarl
ID: 40414927
You're welcome, glad to help! :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to demonstrate how we can use conditional statements using Python.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

29 Experts available now in Live!

Get 1:1 Help Now