regex: extract LDAP cn value from dn string

Regex snippet to extract the cn value from a dn string for LDAP:

Input:

cn=smit1057,ou=ACTIVE,ou=PEOPLE,o=DATA

Target Output:

smit1057

Regex:

/^cn=([^,]+)/$1/

Ruby:

def get_id_from_cname(cname:)
  match = /^cn=([^,]+)/.match(cname)
  return match[1] if match.is_a?(MatchData) && match.length > 0
  return false
end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s