Solved

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

Posted on 2014-10-30
3
561 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
map related example 6 55
configure dependency in POM for new database 3 34
Problem to refer to value 8 62
Unhandled exception type Exception 18 42
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
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…
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.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

840 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