?
Solved

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

Posted on 2014-10-30
3
Medium Priority
?
569 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 2000 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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

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.
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

765 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