Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

747 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

11 Experts available now in Live!

Get 1:1 Help Now