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.
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 boundaryOU=
– a literalOU=
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=)[^,\\]*(?:\\.[^,\\]*)*