Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex to extract certifcate component containing comma from a String in Java

I am looking for a regex that can parse the following String value:

CN=Entrust Certification Authority - L1K,OU=(c) 2012 Entrust\, Inc. - for authorized use 
only,OU=See www.entrust.net/legal-terms,O=Entrust\, Inc.,C=US

The following regex works fine for attributes without a comma in the name:

C=(.*?)(?:,|$)

This will successfully extract the value US from this String. The problem begins with the OU values.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Does anybody have an idea on how to retrieve this value that contains a comma?

>Solution :

You can use

\bOU=([^,\\]*(?:\\.[^,\\]*)*)

See the regex demo.

Details:

  • \b – a word boundary
  • OU= – a literal OU= string
  • ([^,\\]*(?:\\.[^,\\]*)*) – Group 1: any zero or more chars other than a \ and , and then zero or more repetitions of any escaped char (other than a newline char, if you need to also match \n, prepend your pattern with (?s)) and then zero or more chars other than \ and ,.

Here is a variation of the same regex with a lookbehind:

(?<=\bOU=)[^,\\]*(?:\\.[^,\\]*)*
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading