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,O=Entrust\, Inc.,C=US

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


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

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

>Solution :

You can use


See the regex demo.


  • \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:


Leave a Reply