1

The following are Active Directory group Distinguished Names. I want to seperate out the leftmost name from the rest of the DN, as follows:

CN=CTX_APP_Bytemobile_UPM,OU=EEGroups,OU=EEOU,DC=ssa,DC=oam,DC=uk,DC=tmo = CTX_APP_Bytemobile_UPM

CN=OSGRP_IP_Transport,OU=EEGroups,OU=EEOU,DC=ssa,DC=oam,DC=uk,DC=tmo = OSGRP_IP_Transport

CN=Remote Desktop Users,CN=Builtin,DC=ssa,DC=oam,DC=uk,DC=tmo = Remote Desktop Users

So far the RegEx I have only matches the '_' strings. My RegEx is:

(?<=CN=)\w*

I'm also trying to figure out how to use the 're' module in Python. Currently my command is:

presplit = "CN=CTX_APP_Bytemobile_UPM,OU=EEGroups,OU=EEOU,DC=ssa,DC=oam,DC=uk,DC=tmo"
x = re.search("(?<=CN=)\w*", presplit)
print(x) >>> *returns* <re.Match object; span=(3,15), match='CTX_APP_Bytemobile'>

I want to obtain the match as a new string.

Thanks in advance.

1
  • Try: x = re.search("(?<=CN=)[\w\s]*", presplit) Commented Mar 1, 2019 at 17:41

4 Answers 4

2

Seems like you could do this without regex and just use str.split instead. For example:

s = 'CN=CTX_APP_Bytemobile_UPM,OU=EEGroups,OU=EEOU,DC=ssa,DC=oam,DC=uk,DC=tmo'

result = s.split(',')[0].split('=')[1]
print(result)
# CTX_APP_Bytemobile_UPM
Sign up to request clarification or add additional context in comments.

1 Comment

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. This is such a great example.
1

Try: x = re.search("(?<=CN=)[\w\s]*", presplit)

Comments

1

If you just want what is after the equals to the comma you could just split on the comma, set a variable to the position of the equal sign plus one and read it until the end.

presplit = "CN=CTX_APP_Bytemobile_UPM,OU=EEGroups,OU=EEOU,DC=ssa,DC=oam,DC=uk,DC=tmo"
#Make a list of strings split on the comma
lst = presplit.split(",")
#Iterate through the list
for i in lst:
    #Set the starting position to where the equal sign is plus one
    strt = re.search("=", i).start()+1
    #Get the string from the character after the equal sign to the end of the string
    print(i[strt:])

Comments

1

Your code is almost there except for extracting matched string, you need to access it using .group() for full match. Following is your modified code,

import re

presplit = "CN=CTX_APP_Bytemobile_UPM,OU=EEGroups,OU=EEOU,DC=ssa,DC=oam,DC=uk,DC=tmo"
x = re.search("(?<=^CN=)\w+(?:\s+\w+)*", presplit)
if(x):
 print(x.group())

Which prints following like you expected,

CTX_APP_Bytemobile_UPM

Also, for capturing spaces as well, you need to use this regex,

(?<=^CN=)\w+(?:\s+\w+)*
            ^^^^^^^^^^^ This additional part enables it to optionally capture space followed by some word characters

If you notice, I have placed a ^ before CN= in positive look behind so it doesn't match other CN= in middle of the string and just CN in start of line.

Check this demo as well

Besides your positive look behind regex, you can use another simple regex, which is almost 20 times faster then look behind one but uses capture group. This ([^,]+) part in following regex captures the name you are interest in.

^[^=]*=([^,]+)

This one is 20 times faster than earlier one

Check out this Python code,

import re

arr=['CN=CTX_APP_Bytemobile_UPM,OU=EEGroups,OU=EEOU,DC=ssa,DC=oam,DC=uk,DC=tmo = CTX_APP_Bytemobile_UPM','CN=OSGRP_IP_Transport,OU=EEGroups,OU=EEOU,DC=ssa,DC=oam,DC=uk,DC=tmo = OSGRP_IP_Transport','CN=Remote Desktop Users,CN=Builtin,DC=ssa,DC=oam,DC=uk,DC=tmo']

for s in arr:
 m = re.search(r'^[^=]*=([^,]+)', s)
 if (m):
  print(s,'-->',m.group(1))

Prints,

CN=CTX_APP_Bytemobile_UPM,OU=EEGroups,OU=EEOU,DC=ssa,DC=oam,DC=uk,DC=tmo = CTX_APP_Bytemobile_UPM --> CTX_APP_Bytemobile_UPM
CN=OSGRP_IP_Transport,OU=EEGroups,OU=EEOU,DC=ssa,DC=oam,DC=uk,DC=tmo = OSGRP_IP_Transport --> OSGRP_IP_Transport
CN=Remote Desktop Users,CN=Builtin,DC=ssa,DC=oam,DC=uk,DC=tmo --> Remote Desktop Users

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.