From ee2eb274a42a05518f2bc26fa8af816dde271d63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Sch=C3=BCrmann?= Date: Wed, 28 Jan 2015 15:14:38 +0100 Subject: SplitUserID into lib --- src/org/openintents/openpgp/util/OpenPgpUtils.java | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/org/openintents/openpgp/util/OpenPgpUtils.java b/src/org/openintents/openpgp/util/OpenPgpUtils.java index 7e3007d..a5e0163 100644 --- a/src/org/openintents/openpgp/util/OpenPgpUtils.java +++ b/src/org/openintents/openpgp/util/OpenPgpUtils.java @@ -73,4 +73,39 @@ public class OpenPgpUtils { } return hexString; } + + private static final Pattern USER_ID_PATTERN = Pattern.compile("^(.*?)(?: \\((.*)\\))?(?: <(.*)>)?$"); + + /** + * Splits userId string into naming part, email part, and comment part + * + * @param userId + * @return array with naming (0), email (1), comment (2) + */ + public static String[] splitUserId(String userId) { + String[] result = new String[]{null, null, null}; + + if (userId == null || userId.equals("")) { + return result; + } + + /* + * User ID matching: + * http://fiddle.re/t4p6f + * + * test cases: + * "Max Mustermann (this is a comment) " + * "Max Mustermann " + * "Max Mustermann (this is a comment)" + * "Max Mustermann [this is nothing]" + */ + Matcher matcher = USER_ID_PATTERN.matcher(userId); + if (matcher.matches()) { + result[0] = matcher.group(1); + result[1] = matcher.group(3); + result[2] = matcher.group(2); + } + + return result; + } } -- cgit v1.2.3