0

I have a python function that outputs/prints the following:

['CN=*.something1.net', 'CN=*.something2.net', 'CN=*.something4.net', 'CN=something6.net', 'CN=something8.net', 'CN=intranet.something89.net', 'CN=intranet.something111.net, 'OU=PositiveSSL Multi-Domain, CN=something99.net', 'OU=Domain Control Validated, CN=intranet.something66.net',...etc] 

I am trying to extract all the sub-domain names between "CN=" and the single quotation mark, using the split() method in python. I've tried split('CN=', 1)[0] but i can't get my head around on how to use it

what i want to print out:

['something1.net', 'something2.net', 'something4.net', 'intranet.something111.net', 'intranet.something66.net']

Any help would be gratefully appreciated :-)

Thanks, MJ

3 Answers 3

1

The last single quote is indicating the end of the string, so it seems you just want everything after CN=. Assuming that's the case, you can just chop off the first three characters:

subdomains = [item[3:] for item in my_list if item.startswith('CN=')]
Sign up to request clarification or add additional context in comments.

Comments

0

Here is a more readable code that extracts the subdomains in more clean or better way; @tzaman code didn't really give me subdomains.

myDirtyDomains = ['CN=*.something1.net', 'CN=*.something2.net', 'CN=*.something4.net',\
'CN=something6.net', 'CN=something8.net', 'CN=intranet.something89.net',\
 'CN=intranet.something111.net', 'OU=PositiveSSL Multi-Domain', \
 'CN=something99.net', 'OU=Domain Control Validated', 'CN=intranet.something66.net']

cleanSubDomainsList = []

for item in myDirtyDomains:
    countDots = item.count(".")
    if countDots == 2:
        host = item.partition('CN=')[2]
        subdomain = host.partition('.')[0]
        cleanSubDomainsList.append(subdomain)

print(cleanSubDomainsList)

Comments

0

If you just want to strip CN= from each string, you can strip from the left with str.lstrip():

subdomains = [item.lstrip("CN=") for item in my_list]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.