Fantastic, <div>Thanks Hugo that makes 100% sense now! Testing both regex for including / and doing split and when done throwing both away and using the default module.</div><div><br></div><div>Regards<br><br><div class="gmail_quote">
On Wed, Oct 12, 2011 at 2:49 PM, Hugo Arts <span dir="ltr"><<a href="mailto:hugo.yoshi@gmail.com">hugo.yoshi@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">On Wed, Oct 12, 2011 at 3:41 PM, Gerhardus Geldenhuis<br>
<<a href="mailto:gerhardus.geldenhuis@gmail.com">gerhardus.geldenhuis@gmail.com</a>> wrote:<br>
> Hi<br>
> I wrote the following code:<br>
> f = open('/etc/passwd', 'r')<br>
> users = f.read()<br>
> userelements = re.findall(r'(\w+):(\w+):(\w+):(\w+):(\w+):(\w+):(\w+)',<br>
> users)<br>
> print userelements<br>
> for user in userelements:<br>
> (username, encrypwd, uid, gid, gecos, homedir, usershell) = user #<br>
> unpack the tuple into 7 vars<br>
> print username<br>
><br>
> but I get no results so my parsing must be wrong but I am not sure why.<br>
> Incidentally while googling I found the<br>
> module <a href="http://docs.python.org/library/pwd.html" target="_blank">http://docs.python.org/library/pwd.html</a> which I will eventually use<br>
> but I am first curious to fix and understand the problem before I throw away<br>
> this code.<br>
> Regards<br>
><br>
<br>
</div></div>the homedir and usershell parts are paths. Paths will contain slashes.<br>
The \w character class captures only [A-Za-z0-9_], that is, letters,<br>
numbers, and the underscore. That means slashes will not match, and so<br>
the entire match fails.<br>
<br>
On another note, the structure of the /etc/passwd file is pretty<br>
simple, I don't think you need regexes. Simply use split:<br>
<br>
users = f.readlines()<br>
for user in users:<br>
(username, encrypwd, uid, gid, gecos, homedir, usershell) = user.split(':')<br>
<br>
HTH,<br>
<font color="#888888">Hugo<br>
</font></blockquote></div><br><br clear="all"><div><br></div>-- <br>Gerhardus Geldenhuis<br>
</div>