Let me start by saying that this is a matter of aesthetics. I solved my problem, I'm just wondering how to do it.
So, I have a certificate DN, something like this:
CN = Jimmy Blooptoop, OU = Someplace, OU = Employees, DC = Bloopsoft-Inc
Now I want to grab CN from this. There is no built-in support in Java for capturing everything except the full DN from an X509 certificate, without using any third-party library such as bouncy castle - which I cannot use. Therefore, I have to make it out, that there are not many problems. The only thing that confuses a bit is that CN will not always be formatted as <first name> <last name> . Most often it will be <last name>, <first name> <middle initial> . So, in the above example, CN can be Jimmy Bluptop or Bluptotop, Jimmy J (of course, for Joop).
After viewing and regexing, I wrote the following, which works quite well:
Matcher m = Pattern.compile("CN=[A-Za-z]*[, ]*[ A-Za-z]*").matcher(dn); if (m.find()) cn = m.group();
I'm just wondering if there are expressions that would look smaller than shit. I'm pretty sure there is, since I worked on this after reading only the introduction to the regex.
java regex
JDS Oct 28 '11 at 18:45 2011-10-28 18:45
source share