aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDominik Schürmann <dominik@dominikschuermann.de>2015-01-28 15:14:38 +0100
committerDominik Schürmann <dominik@dominikschuermann.de>2015-01-28 15:14:38 +0100
commitee2eb274a42a05518f2bc26fa8af816dde271d63 (patch)
treec1f807e52bb336cb49e830fb79b119f20efcc1f2
parent9509cfbb986ba8d28e1031b3600d07ff3014eea3 (diff)
downloadopenpgp-api-ee2eb274a42a05518f2bc26fa8af816dde271d63.tar.gz
openpgp-api-ee2eb274a42a05518f2bc26fa8af816dde271d63.tar.bz2
openpgp-api-ee2eb274a42a05518f2bc26fa8af816dde271d63.zip
SplitUserID into lib
-rw-r--r--src/org/openintents/openpgp/util/OpenPgpUtils.java35
1 files changed, 35 insertions, 0 deletions
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@example.com>"
+ * "Max Mustermann <max@example.com>"
+ * "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;
+ }
}