Solved

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

Posted on 2014-10-30
3
566 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 36

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 36

Expert Comment

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

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 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