Regular Expression
1. Search by specific head and end, but don’t include them import re reg = '(?<=CN=)[A-Za-z-]*(?=,)' s = "CN=Okta-PagerDuty-RG,OU=Okta Applications,OU=Security Groups,OU=RG New,DC=win,DC=int,DC=rg,DC=com,DC=au" re.search(reg, s).group() # it will return 'Okta-PagerDuty-RG'