re = /(?<=(?:\/CN|,OU)=)([^,]*)(?=(?:,[^,]*)*,DC)/m
str = 'LDAP://abc.123.net/CN=SERVER123ABC,CN=Servers,OU=Test OU,OU=Test OU 2,DC=abc,DC=123,DC=net'
# Print the match result
str.scan(re) do |match|
puts match.to_s
end
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html