software-mariodiana
8/22/2014 - 7:08 PM

A regular expression example in Java.

A regular expression example in Java.

/* http://www.regexplanet.com/advanced/java/index.html */

import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class SplitString {
    public static void main(String[ ] args) {
        // String simulates an example session cookie value.
        String line = "id:_983y4oihdf98y23jkhd9yiohsef|user:9001|expiry:1408737520.878463";
        // Parentheses begin numbering with 1.
        Pattern re = Pattern.compile("^id:(.+)?\\|user:(.+)?\\|expiry:(.+)$");
        Matcher m = re.matcher(line);
        if (m.find()) {
            System.out.println("ID: " + m.group(1));
            System.out.println("User: " + m.group(2));
            System.out.println("Expiry: " + m.group(3));
        }
    }
}