[Tutor] Continue Matching after First Match
Tom Tucker
tktucker at gmail.com
Sat May 19 03:15:25 CEST 2007
Disregard! Looks like part of my problem is the regex string.
On 5/18/07, Tom Tucker <tktucker at gmail.com> wrote:
>
> Please forgive the colors, just trying to help illustrate my question.
>
> The below code snipet works as designed, however the regex matches once
> and exits. I want it to continue matching and printing until EOF. Any
> suggestions?
>
> Why the cStringIO stuff? The input data shown below is collected from
> os.popen. I was trying to find an easy way of matching my regex.
> Matching with a string seemed easier than looping through the ouput
> collected. Hmm. Come to think of it, I guess I could match on the first
> "^dn" catpure that output and then keep looping until "^cn:" is seen. Then
> repeat.
>
> Anyways, any suggestions to fix the below code?
>
> Thanks for the help,
>
>
>
> Code Snipet
> ###########
> multi_regex = re.compile(r'dn: uid=(\w+)..*cn: ((\w+ \w+)|(\w+ \w+\.
> \w+))..*(?=dn:)', re.MULTILINE| re.DOTALL)
> output = os.popen(command).readlines()
> voutput = cStringIO.StringIO()
> for line in output:
> voutput.write(line)
> contents = voutput.getvalue() #<-- contents is <type 'str'>
> match = re.search(multi_regex, contents)
> if match:
> print match.group(1)
> print match.group(2)
>
>
>
> Blue = is what the regex matches
> Red = regex groups # match.group(#)
> Green = next regex groups not being captured
>
> INPUT Data
> ###########
> version: 1
> dn: uid= jtucker,ou=people,dc=companyA,dc=com
> host: hostA
> host: hostB
> host: hostC
> description: other
> gecos: John Tucker
> gidNumber: 1
> uidNumber: 1157
> sn: Tucker
> cn: John Tucker
> uid: jtucker
> objectClass: top
> objectClass: account
> objectClass: person
> objectClass: posixAccount
> objectClass: shadowAccount
> objectClass: inetorgperson
> objectClass: organizationalPerson
> loginShell: /usr/bin/ksh
> homeDirectory: /home/jtucker
> dn: uid=ttucker ,ou=people,dc=companyA,dc=com
> loginShell: /usr/bin/zsh
> host: hostZ
> host: hostC
> uid: ttucker
> cn: Tom Tucker
> sn: Tucker
> objectClass: top
> objectClass: account
> objectClass: person
> objectClass: posixAccount
> objectClass: shadowAccount
> objectClass: inetorgperson
> objectClass: organizationalPerson
> uidNumber: 108
> gidNumber: 102
> gecos: Tom Tucker
> homeDirectory: /home/ttucker
> description: system
>
>
> Current OUTPUT
> #################
> jtucker
> John Tucker
>
>
> Desired OUTPUT
> ################
> jtucker
> John Tucker
> ttucker
> Tom Tucker
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070518/342c01a4/attachment.html
More information about the Tutor
mailing list