0

I have this String extracted from a body of an e-mail

"DESTINATION: 71222222 PRIORITY: urgent AUTRE: rien"

I am looking to extract the strings "71222222", "urgent" and "rien".

I have tried the class Scanner but it did not work.

Which is the best solution to do it in Java?

4
  • Are there newline at the end of each of them?? Commented Oct 4, 2012 at 10:51
  • 1
    Have a look at .split(" "). Commented Oct 4, 2012 at 10:51
  • Welcome to Stack Overflow! We encourage you to research your questions. If you've tried something already, please add it to the question - if not, research and attempt your question first, and then come back. Commented Oct 4, 2012 at 10:52
  • 2
    How did you use Scanner? Seems like Scanner could be used for this... Commented Oct 4, 2012 at 10:53

5 Answers 5

3

You can use regular expressions, like this:

String s = "#DESTINATION: 71222222\n"+
    "#PRIORITY: urgent\n"+
    "#AUTRE: rien";
Pattern p = Pattern.compile("(?<=: )[^ ]+$", Pattern.MULTILINE);
Matcher m = p.matcher(s);
while(m.find()) {
    System.out.println(m.group(0));
}

The core of this solution is this regular expression: (?<=: )[^ ]+$. It matches a sequence of non-space characters that follow colon+space ": " sequence all the way to the end of line $.

Demo on ideone: link.

Sign up to request clarification or add additional context in comments.

Comments

1

try this

String text = "DESTINATION: 71222222 PRIORITY: urgent AUTRE: rien";

String[]lines = text.replace(": ", ":").split(" ");     
String[]values = new String[lines.length];
int i=0;
for(String line : lines){
   values[i++] = line.split(":")[1];
}
System.out.println(Arrays.toString(values));

the output is

[ 71222222,  urgent,  rien]

you can also try this

String text = "DESTINATION: 71222222 PRIORITY: urgent AUTRE: rien";

StringTokenizer tokinizer = new StringTokenizer(text,": ");

while(tokinizer.hasMoreTokens()) {
    tokinizer.nextToken();  //skip first
    System.out.println(tokinizer.nextToken());
}

2 Comments

the new line may not be guaranteed to be "\n" may be "\r"
the email string has been changed anyway
1

You can definitely use a Scanner to extract the information you need:

final String input = "DESTINATION: 71222222 PRIORITY: urgent AUTRE: rien";

final Scanner scanner = new Scanner(input).useDelimiter("\\w+: ");

while(scanner.hasNext()) {
    System.out.println(scanner.next());
}

This prints:

71222222 
urgent 
rien

Comments

0

Try this. It uses regex to split the string by white space (\s)

        String str  = "DESTINATION: 71222222 PRIORITY: urgent AUTRE: rien";
        String[] split = str.split("\\s");
        System.out.println(split[1]+" - "+split[3]+" - "+split[5]);

Comments

0

Correction: when the question first came in the input string included line-feeds. It looks like now the question has been edited to remove the line-feeds. If they are no longer there then my solution won't work.

My first thought is to write a parsing routine that creates a map of key/value pairs. Then you can fetch the data pieces by name and the order in the string doesn't matter.

import java.util.HashMap;
import java.util.Map;

class Main
{

    /**
     * This method parses a string of "key:value" pairs into a map. Parameter
     * pairs are separated by new-lines.
     * @param in the string to parse
     * @return the data map
     */
    static Map<String,String> parseParameters(String in) {

        Map<String,String> params = new HashMap<String,String>();

        int pos = 0;
        while(pos<in.length()) {
            int i = in.indexOf("\n",pos);
            if(i<0) i=in.length();
            String sub = in.substring(pos,i);
            pos = i+1;
            int j = sub.indexOf(":");
            if(j>=0) {
                String key = sub.substring(0,j).trim();
                String val = sub.substring(j+1).trim();
                //System.out.println(key+":"+val);
                params.put(key,val);
            }
        }

        return params;
    }

    public static void main( String args[] )
    { 
        String in = "#DESTINATION: 71222222\n#PRIORITY: urgent\n#AUTRE: rien";

        Map<String,String> params = parseParameters(in);

        // Get the data pieces by name
        System.out.println(params.get("#AUTRE"));
        System.out.println(params.get("#DESTINATION"));
        System.out.println(params.get("#PRIORITY"));

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.