Let me begin by stating that this is a question of aesthetics. I've solved my own problem, I'm just curious about better ways of doing it.
So, I've got a certificate DN, something like this:
CN=Jimmy Blooptoop,OU=Someplace,OU=Employees,DC=Bloopsoft-Inc
Now, I want to grab the CN out of that. In java, there isn't native support to grab anything but the full DN from the X509 certificate without using some 3rd party library like bouncy castle - which I can't use. So I've got to parse it out, which isn't much of problem. The only thing that makes it slightly tricky is the fact that the CN isn't always going to be formatted as <first name> <last name>
. More often than not, it's actually going to be <last name>, <first name> <middle initial>
. So, in the example above, the CN could be Jimmy Blooptoop or Blooptoop, Jimmy J (short for the Joop of course).
After going and reading up about about regular expressions, I wrote the following, which works well enough:
Matcher m = Pattern.compile("CN=[A-Za-z]*[, ]*[ A-Za-z]*").matcher(dn);
if (m.find())
cn = m.group();
I'm just curious if there are expressions that would look less like crap. I'm fairly confident that there are since I worked that out after reading just an introduction to regex.